Sessions
在线手册:中文 英文
PHP手册

简介

Session support in PHP consists of a way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site.

A visitor accessing your web site is assigned a unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL.

The session support allows you to store data between requests in the $_SESSION superglobal array. When a visitor accesses your site, PHP will check automatically (if session.auto_start is set to 1) or on your request (explicitly through session_start() or implicitly through session_register()) whether a specific session id has been sent with the request. If this is the case, the prior saved environment is recreated.

Caution

If you turn on session.auto_start then the only way to put objects into your sessions is to load its class definition using auto_prepend_file in which you load the class definition else you will have to serialize() your object and unserialize() it afterwards.

$_SESSION (and all registered variables) are serialized internally by PHP using the serialization handler specified by the session.serialize_handler ini setting, after the request finishes. Registered variables which are undefined are marked as being not defined. On subsequent accesses, these are not defined by the session module unless the user defines them later.

Warning

Because session data is serialized, resource variables cannot be stored in the session.

Note:

Please note when working with sessions that a record of a session is not created until a variable has been registered using the session_register() function or by adding a new key to the $_SESSION superglobal array. This holds true regardless of if a session has been started using the session_start() function.

Note:

PHP 5.2.2 introduced an undocumented feature to store session files in "/tmp" even if open_basedir was enabled and "/tmp" is not explicitly added to the allowed paths list. This feature has been removed from PHP as of PHP 5.3.0.


Sessions
在线手册:中文 英文
PHP手册
PHP手册 - N: 简介

用户评论:

payal at radixweb dot com (09-Nov-2010 06:39)

If you use auto start session, Session cookie will not be created , it will be created only if you will use session_start()

ivenms at ivenms dot com (23-Mar-2009 08:08)

Here is a sample code which can be used to create logged sessions:

Code for differentiating Guest and Logged members:
<?php
// Starting the session
session_start();

if(isset(
$_SESSION['user']))
    {
       
// Code for Logged members

        // Identifying the user
       
$user = $_SESSION['user'];
       
       
// Information for the user.
   
}
else
    {
       
// Code to show Guests
   
   
}
?>

Code for Logging a User:
<?php
//Username Stored for logging
define("USER", "user");

// Password Stored
define("PASS", "123456");

// Normal user section - Not logged ------
       
if(isset($_REQUEST['username']) && isset($_REQUEST['password']))
            {
               
// Section for logging process -----------
               
$user = trim($_REQUEST['username']);
               
$pass = trim($_REQUEST['password']);
                if(
$user == USER && $pass == PASS)
                    {
                       
// Successful login ------------------
                       
                        // Setting Session
                       
$_SESSION['user'] = USER;
                       
                       
// Redirecting to the logged page.
                       
header("Location: index.php");
                    }
                else
                    {
                       
// Wrong username or Password. Show error here.
                       
                   
}
               
            }
?>
User can be logged successfully with username as "user" and password as 123456.

[NOTE BY danbrown AT php DOT net: Contains a bugfix by (pprem AT pprem DOT net) on 26-APR-2009 with the following note: Dont' write if($user == USER && $pass = PASS).]