How to use php sessions? Session usage

Source: Internet
Author: User
Tags http cookie session id reset sessions setcookie first row

How to use session
The session_start () function must be called before session-related operations ();

It is very easy to pay for the session, such:


Program code

The code is as follows: Copy code

<? 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 directly pay the value for the session:


Program code

The code is as follows: Copy code

<? PHP
Session_Start ();
$ _ SESSION ["name"] = "value ";
?>

You can cancel the session as follows:

Program code

The code is as follows: Copy code

<? Php
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 allows you to easily access the set session variable.

Example:

The code is as follows: Copy code
<? Php
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 call succeeds, the logical value true is returned.

Example:

The code is as follows: Copy code
<? 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.

Example:

The code is as follows: Copy code

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

The code is as follows: Copy code

Setcookie: send an HTTP cookie to the client.
<? 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.

The code is as follows: Copy code

// Save for one day
<? Php
$ 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.

The code is as follows: Copy code

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