Cookie and Session Comparison

Source: Internet
Author: User
Tags set cookie

The biggest difference between a session and a cookie is:
First, the session is to save the session information on the server, and through a session ID to pass the client's information, while the server received the session ID, according to this ID to provide relevant sesion information resources
Second, the cookie is to save all the information in the form of text in the client, and is managed and maintained by the browser
Third, because the session is the server storage, all remote users cannot modify the contents of the session file, and the cookie
For the client storage, all sessions are more secure than cookies, of course, there are many advantages, such as easy control, can be customized according to customer storage (stored in the database) ...

Set Cookie Method
Setcookie ("name", ' Zhangsan ');
Setcookie ("name", ' Zhangsan ', Time () +60);//Set cookie valid for 60 seconds

Read Cokie method
$name =$_cookie["name"];
Delete Cookie method
Setcookie ("name", "", Time ()-1);//Set Cookie () to the current time minus 1,time () function to return the current timestamp in seconds, minus 1 seconds for the expiration time to get past the time, thus deleting the cookie
Deleting Cookiez just needs to set the second parameter in the Setcookie () function to a null value, setting the expiration time of the third parameter cookie to less than the current time of the system


Method of setting Session 1
Session_Start ();
$_session["admin"]= $name;
$_session[' user ']=$_post[' user ';
Method of setting Session 2
Session_register ()//Use this will not need to call session_start (); PHP implicitly calls the Session_Start () function after registering the variable, but requires the option to set the php.ini file to invoke the Session_Start () function;
Session use case
if (!empty ($_session[' session_name ')) {
Equivalent to Isset ($_session[' think ' [' name ']);
$myvalue =$_session[' Session_name '];
}
Delete multiple sessions
$_session=array ();
End the current session;
unset ($_session[' user ');
Equivalent to session (' name ', null);
Session_destroy ();//Delete all current session variables

Cookie and Session Comparison

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.