Instance 11: displays the date stamp and date
Copy codeThe Code is as follows:
<? Php
Echo time (); // returns a string of timestamp in seconds.
Echo "<br> ";
Echo date ("Y-m-d H: I: s", time () + 8*3600); // format time, + 8*3600 to China time Zone
Echo "<br> ";
$ Str = "10:26:10 ";
Echo date ("Y-m-d H: I: s", strtotime ($ str); // convert string to Timestamp
Echo "<br> ";
?>
Example 12: form variable encoding and receiving
Copy codeThe Code is as follows:
<? Php
Echo $ str = urlencode ("first page"); // URL variable encoding, same as google
Echo "<br>". urldecode ($ str); // URL variable decoding, I see
Echo "<br> <a href = index. php? Page = ". $ str."> page 1 </a> ";
Echo "<br> ";
If ($ _ GET) echo "the variable has been received :". $ _ GET ['page']; // $ _ GET corresponds to $ _ POST. PHP automatically recognizes URL encoding and decodes it.
Echo "<br> ";
?>
Instance 13: session usage
Copy codeThe Code is as follows:
<? Php
Session_start (); // The session will expire immediately after the browser is closed. You need to declare it to read and write the session.
$ _ SESSION ['id'] = 'server'; // This is the declaration and initialization, the same as the array usage.
$ _ SESSION ['name'] = 'session ';
Print_r ($ _ SESSION); // an array is output after SESSION registration is successful.
Echo "<br> ";
$ _ SESSION ['id'] = 'or Server'; // change a SESSION and output it.
Unset ($ _ SESSION ['name']); // cancel a SESSION and output it.
Print_r ($ _ SESSION );
Echo "<br> ";
?>
Instance 14: cookie usage
Copy codeThe Code is as follows:
<? Php
Setcookie ("id", "client"); // This is the declaration and cookie initialization function. The browser is disabled.
Setcookie ("name", "session", time () + 3600); // expires one hour later
Print_r ($ _ COOKIE); // an array is output after the session registration is successful. You can also access it using $ HTTP_COOKIE_VARS.
Echo "<br> ";
Setcookie ("id", "or client"); // change a session and output it.
Unset ($ _ COOKIE ['name']); // cancel a session and output it. This is equivalent to setcookie ("name", "", time ()-1 ); this is what many books have said. In fact, unset can also be used to log out.
Print_r ($ _ COOKIE );
Echo "<br> ";
?>