The data saved in the Cookie,session file is created as a variable in the PHP script, and the session variable created can be referenced by a cross-page request in the life cycle (20 minutes). In addition, the session is stored on the server side of the conversation, relatively safe, not like a cookie has a storage length limit.
How the session works
When a session is started, a random session_id is generated, that is, the file name of the session, at which time session_id is stored in the server's memory, when the page is closed, the ID will be automatically logged off, re-login to this page, A random session_id is regenerated.
function of Session
Session in Web technology is very important, because the Web page is a stateless connection program, it is not possible to know the user's browsing status. The session allows you to record information about the user so that the user can confirm the request to the Web server again as that identity. For example: When users browse e-commerce sites, if there is no session, then users need to enter the account password every time they browse.
In addition, the session is suitable for storing less information. If the user needs to store less information and does not need long-term storage for the stored content, it is appropriate to use the session to store it on the server side.
Create a session
The steps to create the session are as follows:
Start session → Register session → use session → delete session
1, start the session
There are two ways to start a session, one is to use the session_start () function, and the other is to start the session implicitly by using the Session_register () function to log in to a variable for the session.
Create a session with the Sesssion_start () function with the following syntax:
BOOL Session_Start (void);
To create a session by using the Session_register () function
The Session_register () function is used to login a variable for the session to suppress the startup session, but the option to set the php.ini file specifies the register_globals instruction as on, and then restarts the Apache service.
Note
With the Session_register () function, you do not need to call the Session_Start () function, but PHP implicitly calls the Session_Start () function after the variable is registered.
2, registering a session
After the session variables are started, they are all saved in the array session. It is easy to create a session variable from an array _session. Just add an element to the array directly.
The specific instance code is as follows:
3, using session
You first need to determine if a session variable ID exists, create one if it does not exist, and enable it to be accessed through the global array $_session. If it already exists, then the registered session variable is loaded for use by the user.
For example: Determine if the session of the user name is empty, and if it is not empty, assign the conversation variable to $jackvalue.
The instance code is as follows:
4, delete session
(1) Delete a single session
Delete the session variable, as with the array, and simply unregister an element in the $_session array.
Specific as follows:
unset ($_session[' session_name ');
(2) Delete multiple sessions
If you want to unregister all session variables at once, you can assign an empty array to $_session.
The code is as follows:
session= Array ();
(3) ending the current session
If the entire session has ended, you should log off all session variables first, then use the Session_destroy () function to clear the current session and empty all resources in the session to completely destroy it.
The code is as follows:
Session_destroy ();
5, set the expiration time of the session
1, the client does not prohibit cookies
(1) Use Session_set_cookie_params () to set the expiration time of the session, this function is the session with the cookie set failure time, if the session expires after 1 minutes, the instance code is as follows:
Note:
Session_set_cookie_params () must be used before session_start ()
(2) Use the Setcookie () function to set the expiration time of the session
The sample code is as follows:
Where Session_name is the name of the session, SESSION_ID is the session used to identify the client's identity, session_id is a randomly generated unique name, so the session is relatively safe. The expiration time is the same as the expiration time of the cookie, and the last parameter is optional to indicate the path of the cookie.