Use cookie and session in PHP

Source: Internet
Author: User
In PHP, cookies, sessioncookies, and Sessions are widely used in both java and php. cookies can be seen as Client technologies, while Sessions are server technologies. For example, if the shopping cart and automatic website login can be implemented using cookies, the session tends to be used for verification, which is more secure than the cookie because the session is stored in PHP and uses cookies and sessions.

Cookie and session are widely used in java or php. cookie can be regarded as client technology, while session is server technology. For example, you can use cookies to perform automatic logon for shopping carts and websites. session is more inclined to verify this one, which is more secure than cookie because the session is stored on the server and cannot be deleted or modified at will. Below is a simple example of my learning experience.

1. use of cookies

To save the cookie, you can directly use the setCookie function on the php page to save the cookie as follows:

 

The first parameter is the cookie key, the second parameter is value, and the third parameter indicates when the cookie expires in seconds, and time () indicates the current time. This code indicates that the cookie will expire in 2 minutes.

The same is true for cookie update. For Firefox, we can see the following cookies:


Next, delete the cookie.

SetCookie ($ cookiename, ''); or setCookie ($ cookiename, NULL); both methods can delete cookies.

It is easier to obtain the cookie by using $ _ COOKIE. you can also use this predefined ultra-global array to perform batch cookie operations.

2. session usage

(1) enable session

(2) put the object into the session

 session_start();  $_SESSION["password"]="123456";

You can save sessiona in two sentences, as shown below:

To retrieve a session, you can also use $ _ SESSION. to delete a single session, you can use unset ($ _ SESSION ["password"]). if you delete all sessions, you can use session_destroy ();


The real principle of session is not so easy to understand, and it is difficult to understand deeply. if you need to understand it, you can use firebug to view http requests and responses. After the server creates a session, it returns

PHPSESID. this ID is the unique session ID. when the browser client needs to retrieve the session, it will use this unique ID to retrieve the session information from the server. If the client disables the cookie, the session cannot be shared according to the normal code. The two simplest practices are provided here.

The first is URL rewriting. First, determine whether PHPSESSID exists. if so, set session_id (SID value );

The second is to modifySession. use_Trans_ Sid: set the value to 1.


For the first PHP type, a constant named SID can be used directly, because this situation is more extreme than usual. The specific code will not be written. If necessary, you can write comments to me. I will send them directly.

Note that php sessions can also store objects. it is best to use require_once to introduce this object before use.


Finally, for the session application, I found a simple verification code on the internet. the code is as follows:

 

You don't need to explain how to use it.

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.