Session usage is actually very simple. it can save the data submitted by the user in the form of a global variable in a session and generate a unique session_id. this is to avoid production when there is more
Session usage is actually very simple. it can save the data submitted by the user in the form of a global variable in a session and generate a unique session_id. this is to avoid confusion when there is more, in addition, the session can only have one session_id for the same site in the same browser. let's take a look at the session usage methods.
How to use session
For Sessions, the session_start () function must be called before. it is very easy to pay for sessions, such as program code:
-
- Session_start ();
- $ Name = "this is a Session example ";
- Session_Register ("Name"); // do not write it as Session_Register ("$ Name ");
- Echo $ _ SESSION ["Name"];
- // After $ _ SESSION ["Name"] is "this is a Session example"
- ?>
After php4.2, you can directly pay the value for the session. the program code is as follows:
-
- Session_Start ();
- $ _ SESSION ["name"] = "value ";
- ?>
The program code is as follows:
-
- Session_start ();
- Session_unset ();
- Session_destroy ();
- ?>
There is a BUG in canceling a session variable above php4.2.
Read session
PHP's built-in $ _ SESSION variable can easily access the set session variable. the instance code is as follows:
-
- Session_start ();
- Echo "the registered user name is:". $ _ SESSION ["username"]; // The output user name is nostop.
- ?>
Check whether the variable is registered as a session variable
Session_is_registered
Syntax:Boobean session_is_registered (string name );
This function can check whether the specified variable has been registered in the current session. the parameter name is the name of the variable to be checked. if the parameter is successful, the logical value true is returned.
The instance code is as follows:
-
- Session_start ();
- If (! Session_is_registered ("gender") {// determines whether the current session variable is registered
- Session_register ("gender"); // registers the variable
- }
- $ Gender = "female ";
- Echo $ _ SESSION ['gender']; // female
- ?>
Access the current session name
Session_name
Syntax:Boolean session_name (string [name]);
This function can get or reset the name of the current session. if there is no parameter name, it means to get the name of the current session. if a parameter is added, the session name is set to the parameter name. The instance code is as follows:
-
- $ SessionName = session_name (); // Obtain the current Session name. The default value is PHPSESSID.
- $ SessionID = $ _ GET [$ sessionName]; // Obtain the Session ID.
- Session_id ($ sessionID); // use session_id () to set the Session ID.
- ?>
Access the current session ID
Session_id
Syntax: Boolean session_id (string [id]);
This function can obtain or reset the id of the current session. If no parameter id is available, only the id of the current session is obtained, when a parameter is added, the session id is set to the New specified id.
Set the Session lifetime, instanceThe code is as follows:
Setcookie:Send an HTTP cookie to the client.
-
- Session_start
- // Save for one day
- $ LifeTime = 24*3600;
- Setcookie (session_name (), session_id (), time () + $ lifeTime ,"/");
- ?>
Session_set_cookie_params: Set the Session lifetime. this function must be called before the session_start () function is called.
If the client uses IE 6.0, session_set_cookie_params (); the function sets the Cookie. Therefore, we need to manually call the setcookie function to create a cookie. the instance code is as follows:
- // Save for one day
-
- $ LifeTime = 24*3600;
- Session_set_cookie_params ($ lifeTime );
- Session_start ();
- $ _ Session ["admin"] = true;
- ?>
Set the Session file storage path
Session_save_path (): The instance code must be called before the session_start () function is called:
-
- // Set a storage directory
- $ SavePath = "./session_save_dir /";
- // Save for one day
- $ LifeTime = 24*3600;
- Session_save_path ($ savePath );
- Session_set_cookie_params ($ lifeTime );
- Session_start ();
- $ _ Session ["admin"] = true;
- ?>
-
- Session_start (); // start the Session
- $ Username = 'nostop ';
- Session_register ('Username'); // register a variable named username
- Echo 'registered User: '. $ _ SESSION ['username']; // registered User: nostop reads Session variables
- $ _ SESSION ['age'] = 23; // declare a variable named age and assign values
- Echo 'age: '. $ _ SESSION ['age']; // age: 23
- Session_unregister ('Username'); // deregister the Session variable
- Echo $ _ SESSION ['username']; // null
- Echo $ _ SESSION ['age']; // 23
- Unset ($ _ SESSION ['age']); // deregister the Session variable
- Echo 'registered User: '. $ _ SESSION ['username']; // null
- Echo 'age: '. $ _ SESSION ['age']; // null
- ?>
Note:
1: no output is allowed before Session_Start () is called. for example, the following is incorrect.
-
- Session_Start (); // output in the first line
- .....
- ?>
Tip 1:If "... headers already sent..." appears, it means to output information to the browser before Session_Start.
It is normal to remove the output (this error will also occur in the COOKIE, the same reason for the error)
Tip 2:If your Session_Start () is placed in a loop statement and it is difficult to determine where to output information to the browser, you can use the following method:
1 line
... Here is your program ......
2: What is the error?
Warning: session_start (): open (/tmpsess_7d1_aa36b4c5ec13a5c1649cc2da23f, O_RDWR) failed :....
Because you have not specified the path for storing session files.
Solution:
(1) create a folder tmp on drive C
(2) open php. ini, find session. save_path, and change it to session. save_path = "c:/tmp"