Simple explanation of php cookies and sessions (notes)

Source: Internet
Author: User
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"
 
  
 ";}
?

?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.