Php cookie and session simple explanation (note) cookie: the stored value is local, that is, the client. The advantage is that it can be set to save for a long time, but the session is not secure: it is saved on the server, and the browser is closed. for example, there is a text box named "username" on the login page ", after successful login, go to the page (simple explanation of cookie and session in x php (notes)
Cookie: the stored value is local, that is, the client. It can be set to save for a long time, but is not secure.
Session
For example, there is a text box named "username" on the login page. after successful login, what is displayed on the display page (xs. php? Login username, with a text button to exit
The logon page code is as follows:
$ Username = $ _ POST ['username']; // obtain the value in the text box. here, the username setcookie ('username', $ username, time () + 3600 ); // Set the cookie name here, pointing to the set value (that is, pointing to the above user name), and then the storage time is 3600
Display page code :?
Echo "username :". $ _ COOKIE [username]; // Obtain the username echo "quit"; // Click the button to exit the username, the principle is to reset the cookie value to null if ($ _ GET ['out']) {setcookie ('username ',''); // An Empty echo is set here"
"; // Redirect to the logon page again}
?? ? The last echo above is like this: echo "script" location. href = 'login. php' script ";
Session?
? Login page code
Session_start (); // start the session. you must write $ username =$ _ POST ['username'] at the top of the file; $ _ SESSION [username] = $ username;
? Display page:
Session_start (); echo "welcome ". $ _ SESSION [username]; echo "quit"; if ($ _ GET ['out']) {unset ($ _ SESSION [username]); // Here, sessionecho is cleared"
";}
?
?