Home > Article > Backend Development > Automatic login using session and cookie in php_PHP tutorial
Use of session:
session_start(); Define $_SESSION array variable.
Before session_start() is used, there cannot be any form of output, including output in php and html code.
The $_SESSION array cannot use numeric subscripts, only string subscripts.
The session_save_path configuration item in php.ini determines the location where the session is saved.
By default, sessions are saved in files. We can use the session_set_save_handler() function to rewrite the session mechanism.
Set valid time
代码如下 | |
SESSION: ini_set('session.gc_maxlifetime',24*3600);//session保存一天 session_start(); $_SESSION[‘web_name’]='网易'; |
Note: It seems that the default time is: 20-24 minutes, but the session disappears automatically when the browser is closed!
Delete session:
Delete an element unset($_SESSION[key]);
Delete all sessions $_SESSION=array();
Delete the file that saves session data session_destroy();
The session_disstroy() method only deletes the session file on the server side and does not release the $_SESSION variable in the memory. If we var_dump($_SESSION) immediately after session_distroy(), we can still see the session output. Therefore, if you want to completely release the session, you must use $_SESSION=array().
Use of cookies:
Set cookie: setcookie (variable name, variable value, survival time (timestamp)).
Delete cookies: setcookie(variable name, value, time()-1). Principle: Set the cookie's lifetime to expire.
Read cookie: $_COOKIE[variable name].
$path="/"; //Set the path for cookie storage; 1. The default storage is this directory, which can only be accessed in this directory. 2. "/" means it is stored in the root directory, 3. "/foo/ "Only files under the foo folder can be accessed
The code is as follows | |||||
|
Note: The value stored in the cookie can only be of string type, but how to solve the problem of storing numerical values!
Storage:
The code is as follows | |||||
$array=array('a',' b');
|
Reading:
The code is as follows | |||||
//Restore serialize object
$other = StripSlashes($_COOKIE['snsresult ']);//This step must be performed |
Note: When accessing the value of setcookie in the code, you cannot output any content in front of it, nor can it be a blank line, otherwise the cookie will have no value.
1. $_COOKIE can only read the value of the cookie and cannot set the cookie.
2. Before using setcookie, there cannot be any type of output.
3. After the script sets the cookie for the first time, it cannot be obtained by using $_COOKIE in the current script. You need to refresh the page or obtain it in other scripts.
Automatic login
The code is as follows | |
//Check if the user is logged in
|
The connection and difference between the two:
Contact:
Both are called session technologies and are used to solve the stateless nature of http.
SessionID is stored in cookie. By default, the session relies on cookies. If cookies are completely disabled on the browser, the session will not be available. But we have other ways to keep the session useful. For example, in url rewriting, put the session ID in the url; add a hidden field to the form, store the session ID in the hidden field, and send it to the browser.
Difference:
The cookie is saved early in the browser. Every time you access the server, the cookie will be brought to the server to ensure that the server knows that the two requests come from the same client. Less secure.
The session is saved on the server, and each request will be matched by the session ID brought by the cookie to the server. Higher security.
The validity period of the session starts from session_start() and ends when the browser is closed.
Cookies can be set to expire. By default, if the browser closes the cookie, it will become invalid and the session ID will be lost. Even if the session file on the server side is still there, it will not be found.