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