Learn PHP session can be used in many places, such as a background login function, to let the program remember the user's session, in fact, is very simple, read the following article you will understand.
PHP session usage is very simple it can save user submitted data in the form of a global variable in a session and will generate a unique session_id, so that there is no confusion in order to have more, and the session in the same browser the same site can only have one session_id, let's take a look at the session using the method.
How to use the session, usually related to the session, must call the function session_start ();
Assigning a value to a session is simple, such as:
The code is as follows:
<?phpsession_start (); $Name = "This is a session example"; Session_register ("Name");//note, do not write: Session_register ("$Name"); Echo $_session["name"];//after $_session["name"] is "This is a SESSION example"?>
After php4.2, you can assign values directly to the session:
The code is as follows:
<? Phpsession_start (); $_session["name"]= "value";? >
The cancellation session can be done like this:
The code is as follows:
<?phpsession_start (); Session_unset (); Session_destroy ();? >
Read session
PHP built-in $_session variables can be easily accessed by setting the SESSION variable.
The code is as follows:
<?phpsession_start (); echo "Registered user name:". $_session["username"; The output registered user name is:nostop?>
Check if the variable is registered as a session variable session_is_registered
Syntax: Boobean session_is_registered (string name);
This function checks to see if a specified variable has been registered in the current session, and the parameter name is the variable name to check. Success returns the logical value TRUE.
The code is as follows:
<?php session_start (); if (!session_is_registered ("gender")) {//Determines whether the current session variable is registered session_register ("gender"); Register 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 no parameter name is given, the current session name is obtained, plus the parameter indicates that the session name is set to the parameter name.
The code is as follows:
<?php$sessionname = Session_name (); Gets the current Session name, which defaults to Phpsessid$sessionid = $_get[$sessionName]; Get Session idsession_id ($sessionID); Session id?> obtained using session_id () settings
Access the current session ID number session_id
Syntax: Boolean session_id (string [id]);
This function Gets or sets the identification number of the session that is currently stored. The absence of a parameter ID means that only the ID of the current session is obtained, plus the parameter indicates that the session's identification number is set to the newly specified ID.
Set the lifetime of the Session
The code is as follows:
<?php session_start //Save a day $lifeTime = * 3600; Setcookie (Session_name (), session_id (), time () + $lifeTime, "/");? >
Session_set_cookie_params: To set the lifetime of the Session, the function must be called before the session_start () function call.
If the client uses IE 6.0, Session_set_cookie_params (); There are some problems with the function setting cookie, so let's call the Setcookie function manually to create a cookie.
The code is as follows:
<?php//save one day $lifeTime = 24 * 3600; Session_set_cookie_params ($lifeTime); Session_Start (); $_session["Admin"] = true;? >
Set save path for Session file
Session_save_path (): Must be called before the session_start () function call.
The code is as follows:
<?php//Set a storage directory $savePath = "./session_save_dir/"; Save one day $lifeTime = 24 * 3600; Session_save_path ($savePath); Session_set_cookie_params ($lifeTime); Session_Start (); $_session["Admin"] = true;? >
<?phpsession_start (); Start session$username= ' nostop '; Session_register (' username '); Register a user named Username variable echo ' registered: '. $_session[' username ']; Registered User: Nostop reads the SESSION variable $_session[' age ']=23; Declare a variable named age, and assign the value echo ' Ages: '. $_session[' old '; Age: 23session_unregister (' username '); Unregister SESSION variable echo $_session[' username ']; Null echo $_session[' age '];//23unset ($_session[' age '); Unregister SESSION variable echo ' registered User: '. $_session[' username ']; Empty echo ' Age: '. $_session[' ages '; Empty?>
Attention:
1: cannot have any output before calling Session_Start (). For example, the following is an error.
1 rows
2 rows of <? Php
3 lines session_start ();//before the first line already has output
4 lines .....
5 Row?>
Tip 1:
Anything that appears ".... Headers already sent ..." is the output of information to the browser before Session_Start ().
Remove the output is normal, (the cookie will also appear this error, the same reason)
Tip 2:
If your session_start () is placed in a looping statement and it is difficult to determine where to export the information to the browser before, you can use the following method:
1 rows of <? PHP Ob_start ();?>
........ Here is your program ...
2: What is wrong with this
Warning:session_start (): Open (/tmpsess_7d190aa36b4c5ec13a5c1649cc2da23f, O_RDWR) failed: ....
Because you did not specify a path to store the session file.
Workaround:
(1) Set up folder TMP on C disk
(2) Open php.ini, find Session.save_path, change to Session.save_path= "C:/tmp"