Session of PHP Development

Source: Internet
Author: User
Tags setcookie

The data saved in the Cookie,session file is created as a variable in the PHP script, and the session variable created can be referenced by a cross-page request in the life cycle (20 minutes). In addition, the session is stored on the server side of the conversation, relatively safe, not like a cookie has a storage length limit.

How the session works
When a session is started, a random session_id is generated, that is, the file name of the session, at which time session_id is stored in the server's memory, when the page is closed, the ID will be automatically logged off, re-login to this page, A random session_id is regenerated.

function of Session
Session in Web technology is very important, because the Web page is a stateless connection program, it is not possible to know the user's browsing status. The session allows you to record information about the user so that the user can confirm the request to the Web server again as that identity. For example: When users browse e-commerce sites, if there is no session, then users need to enter the account password every time they browse.
In addition, the session is suitable for storing less information. If the user needs to store less information and does not need long-term storage for the stored content, it is appropriate to use the session to store it on the server side.

Create a session
The steps to create the session are as follows:
Start session → Register session → use session → delete session

1, start the session
There are two ways to start a session, one is to use the session_start () function, and the other is to start the session implicitly by using the Session_register () function to log in to a variable for the session.
Create a session with the Sesssion_start () function with the following syntax:
BOOL Session_Start (void);

To create a session by using the Session_register () function
The Session_register () function is used to login a variable for the session to suppress the startup session, but the option to set the php.ini file specifies the register_globals instruction as on, and then restarts the Apache service.

Note
With the Session_register () function, you do not need to call the Session_Start () function, but PHP implicitly calls the Session_Start () function after the variable is registered.

2, registering a session

After the session variable is started, it is all saved in the array S E S S I ON in. PassevernumberGroup _session Creating a Session variable is easy. Just add an element to the array directly.
The specific instance code is as follows:

<?php session_start();  //启动一个session $_SESSION["admin"] =null;   //声明一个名为admin的变量并赋空值?>

3, using session
You first need to determine if a session variable ID exists, create one if it does not exist, and enable it to be accessed through the global array $_session. If it already exists, then the registered session variable is loaded for use by the user.

For example: Determine if the session of the user name is empty, and if it is not empty, assign the conversation variable to $jackvalue.
The instance code is as follows:

<?phpif(!empty($_SESSION[‘session_name‘])) //判断存储用户会话变量是否为空{$jackValue =$_SESSION[‘session_name‘];//将会话变量赋给一个变量$jackValue}?>

4, delete session
(1) Delete a single session
Delete the session variable, as with the array, and simply unregister an element in the $_session array.
Specific as follows:

unset ($_session[' session_name ');

(2) Delete multiple sessions
If you want to unregister all session variables at once, you can assign an empty array to $_session.

The code is as follows:
S E S S I ON = Array ();

(3) ending the current session
If the entire session has ended, you should log off all session variables first, then use the Session_destroy () function to clear the current session and empty all resources in the session to completely destroy it.
The code is as follows:
Session_destroy ();

5, set the expiration time of the session
1, the client does not prohibit cookies
(1) Use Session_set_cookie_params () to set the expiration time of the session, this function is the session with the cookie set failure time, if the session expires after 1 minutes, the instance code is as follows:

<?php$time =1*60;                       //设置session的失效时间session_set_cookie_params($time);  //是用函数session_start();$_SESSION["userName"]="jack";?>

Note:
Session_set_cookie_params () must be used before session_start ()

(2) Use the Setcookie () function to set the expiration time of the session
The sample code is as follows:

<?phpsession_start();$time =1*60;setcookie(session_name(),session_id(),time()+$time,"/");$_SESSION["user"]="jack";?>

Where Session_name is the name of the session, SESSION_ID is the session used to identify the client's identity, session_id is a randomly generated unique name, so the session is relatively safe. The expiration time is the same as the expiration time of the cookie, and the last parameter is optional to indicate the path of the cookie.

Session of PHP Development

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.