How to Use php session

Source: Internet
Author: User
Tags php session setcookie

PHP 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.
The session_start () function must be called before session-related usage ();
It is easy to assign values to sessions, such:
Copy codeThe Code is as follows:
<? Php
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 assign values to the session directly:
Copy codeThe Code is as follows:
<? PHP
Session_Start ();
$ _ SESSION ["name"] = "value ";
?>

You can cancel the session as follows:
Copy codeThe Code is as follows:
<? Php
Session_start ();
Session_unset ();
Session_destroy ();
?>

Read session

PHP's built-in $ _ SESSION variable allows you to easily access the set session variable.
Copy codeThe Code is as follows:
<? Php
Session_start ();
Echo "the registered user name is:". $ _ SESSION ["username"]; // The output user name is nostop.
?>

Check whether the variable is registered as the 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 call succeeds, the logical value true is returned.
Copy codeThe Code is as follows:
<? Php
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 the parameter name is not set, the current session name is obtained. If the parameter is added, the session name is set to the parameter name.
Copy codeThe Code is as follows:
<? Php
$ 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 number of the currently stored session. If there is no parameter id, only the id of the current session is obtained. If the parameter is added, the id of the session is set to the newly specified id.
Set the Session lifetime
Copy codeThe Code is as follows:
<? Php
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 the cookie.
Copy codeThe Code is as follows:
<? Php
// 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 (): it must be called before the session_start () function is called.
Copy codeThe Code is as follows:
<? Php
// 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;
?>

<? Php
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.


1 line
2 rows <? PHP
3 rows Session_Start (); // output already exists in the first row
4 rows .....
5 rows?>

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 <? PHP Ob_Start ();?>
... 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"

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.