Session comparison cookie in php Development. the data stored in the session file is created in the php script in the form of variables. the created session variable is in the life cycle (20 minutes) can be referenced by cross-page requests. In addition, the session is stored on the server side, which is relatively secure and does not have storage length restrictions like cookies.
How the session works
When a session is started, a random session_id is generated, that is, the session file name. at this time, session_id is stored in the server's memory. when the page is closed, the id is automatically logged out, log on to this page again and a random session_id will be generated again.
Session functions
Session is very important in web technology. because a web page is a stateless Connection program, you cannot know the browsing status of the user. Through session, you can record the user's information for the user to confirm when the user submits requirements for the web server in this identity again. For example, if a user does not have a session when browsing an e-commerce website, the user needs to enter the account password for each browsing.
In addition, sessions are suitable for storing a small amount of information. If you need to store less information and do not need to store the stored content for a long time, it is appropriate to use session to store the information on the server.
Create session
To create a session, follow these steps:
Start Session → register session → use session → delete session
1. start the session
You can use the session_start () function or the session_register () function to implicitly start a session by logging on to a variable.
Use the sesssion_start () function to create a session. the syntax format is as follows:
Bool session_start (void );
Use the session_register () function to create a session
The session_register () function is used to implicitly start a session by logging on to a variable for the session. However, you must set the options of the php. ini file to specify the register_globals command as on and then restart the apache service.
Note:
The session_register () function does not need to be called. However, php implicitly calls the session_start () function after the variable is registered.
2. Register a session
After the SESSION variables are started, they are all stored in the array SESSION. It is easy to create a SESSION variable through the array _ SESSION. You only need to add an element to the array.
The specific instance code is as follows:
3. use session
First, you need to determine whether a SESSION variable ID exists. if it does not exist, create one and enable it to access through the Global Array $ _ SESSION. If it already exists, load the registered session variables for your use.
For example, if the session of the user name is null or not, the session variable is assigned to $ jackValue.
The instance code is as follows:
4. delete a session
(1) delete a single session
Delete SESSION variables. just like the operation on the array, you can directly cancel an element in the $ _ SESSION array.
The details are as follows:
Unset ($ _ SESSION ['session _ name']);
(2) delete multiple sessions
To cancel all SESSION variables at a time, you can assign an empty array to $ _ SESSION.
The code is as follows:
SESSION = array ();
(3) end the current session
If the entire session has ended, you should first cancel all session variables, then use the session_destroy () function to clear the current session, clear all resources in the session, and completely destroy the session.
The code is as follows:
Session_destroy ();
5. set the session expiration time.
1. the client does not disable cookies.
(1) use session_set_cookie_params () to set the session expiration time. This function is used to set the session expiration time in combination with the Cookie. if the Session is invalidated after one minute, 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 session expiration time
The sample code is as follows:
Session_name is the name of the session, and session_id is the identifier used by the session to identify the client. session_id is the unique name randomly generated, so the session is relatively secure. The expiration time is the same as the cookie expiration time. the last parameter is optional to specify the cookie path.