Teach you how to use PHP session_php instances

Source: Internet
Author: User
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:
Copy CodeThe code is as follows:
Session_Start ();
$Name = "This is a session example";
Session_register ("Name");//note, do not write: Session_register ("$Name");
Echo $_session["Name"];
After the $_session["Name"] is "This is a SESSION example"
?>

After php4.2, you can assign values directly to the session:
Copy CodeThe code is as follows:
Session_Start ();
$_session["Name"]= "value";
?>

The cancellation session can be done like this:
Copy CodeThe code is as follows:
Session_Start ();
Session_unset ();
Session_destroy ();
?>

Read session

PHP built-in $_session variables can be easily accessed by setting the SESSION variable.
Copy the Code code as follows:
Session_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.
Copy the Code code as follows:
Session_Start ();
if (!session_is_registered ("gender")) {//Determines whether the current session variable is registered
Session_register ("gender"); Registering variables
}
$gender = "female";
echo $_session[' gender ']; Woman
?>

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.
Copy the Code code as follows:
$sessionName = Session_name (); Gets the current Session name, which defaults to PHPSESSID
$sessionID = $_get[$sessionName]; Get Session ID
session_id ($sessionID); Session ID obtained using the session_id () setting
?>

Access current session ID 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 lifetime of Session
Copy code code as follows:
!--? php session_start
//Save one day
$lifeTime = 24 * 3 600;
Setcookie (Session_name (), session_id (), time () + $lifeTime, "/");
?

Session_set_cookie_params: Sets the lifetime of the session, which must be called before the Session_Start () function is called.
If the client is using IE 6.0, Session_set_cookie_params (), the function setting cookie is a bit problematic, so let's call the Setcookie function manually to create the cookie.  The
copy Code code is as follows:
!--? PHP //Save day
$lifeTime = 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.
Copy the Code code as follows:
Set up a storage directory
$savePath = "./session_save_dir/";
Save the day
$lifeTime = 24 * 3600;
Session_save_path ($savePath);
Session_set_cookie_params ($lifeTime);
Session_Start ();
$_session["Admin"] = true;
?>

Session_Start (); Start session
$username = ' nostop ';
Session_register (' username '); Register a variable named username
Echo ' Registered User: '. $_session[' username '; Registered User: Nostop Read session variable

$_session[' age ']=23; Declares a variable named age, and assigns a value
Echo ' Age: '. $_session[' ages '; Age: 23

Session_unregister (' username '); Unregister Session Variables
echo $_session[' username ']; Empty
echo $_session[' age '];//23

unset ($_session[' age '); Unregister Session Variables
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 lines 3 rows 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
........ 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"

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.