PHP programming is the fastest way to understand. Lecture 4: Date, form receiving, session, and cookie. For more information, see. Instance 11: displays the date stamp and date
The code is as follows:
Echo time (); // returns a string of timestamp in seconds.
Echo"
";
Echo date ("Y-m-d H: I: s", time () + 8*3600); // format time, + 8*3600 to China time zone
Echo"
";
$ Str = "10:26:10 ";
Echo date ("Y-m-d H: I: s", strtotime ($ str); // Convert string to timestamp
Echo"
";
?>
Example 12: form variable encoding and receiving
The code is as follows:
Echo $ str = urlencode ("first page"); // URL variable encoding, same as google
Echo"
". Urldecode ($ str); // URL variable decoding, I see
Echo"
Page 1 ";
Echo"
";
If ($ _ GET) echo "the variable has been received :". $ _ GET ['Page']; // $ _ GET corresponds to $ _ POST. PHP automatically recognizes URL encoding and decodes it.
Echo"
";
?>
Instance 13: session usage
The code is as follows:
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"
";
$ _ SESSION ['id'] = 'or server'; // change a SESSION and output it.
Unset ($ _ SESSION ['name']); // cancel a SESSION and output it.
Print_r ($ _ SESSION );
Echo"
";
?>
Instance 14: cookie usage
The code is as follows:
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"
";
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"
";
?>