PHP Session Control

Source: Internet
Author: User
Tags session id php session setcookie
This article introduces the content of the PHP session control, now share to everyone, the need for small partners can refer to


Session control


The HTTP protocol is the protocol that the WEB server communicates with the browser, which is a stateless protocol that is independent of each HTTP request. Therefore, the HTTP protocol does not have a built-in mechanism to maintain the state between two transactions. For example, when a user requests a page and then requests another page, HTTP will not be able to tell us whether the two requests are from the same user.

The commonly used session control technology has cookie and session . Simply put, a Cookie determines the user's identity by recording information in the client, and the Session determines the user's identity by logging information on the server side.

Cookies

A Cookie is a small text file that is contained in an HTTP request message that is passed between the WEB server and the browser. The Cookie works as follows:

  1. The server sets a field in the HTTP response message Set-Cookie and places the Cookie data in the Set-Cookie field with the HTTP datagram to the browser;

  2. After receiving the HTTP response message, the browser checks that the Set-Cookie field has a value and creates a Cookie file locally to hold the data.

  3. When the browser sends a request to the server again, the browser searches for the locally saved cookie file, and if there is any cookie in the cookie file that is related to the URL being connected, set a cookie field in the HTTP request message and the number in the cookie file It is added to this field, and the HTTP request message that carries the Cookie field is finally sent to the server.

Cookies can be used to save simple information such as user names, passwords, personalization settings, and the following are instructions for using cookies:

Create

<?phpsetcookie ("Cookie", "Cookievalue", Time () +3600);
Setcookie () must be called before the content output of the HTML file

Receive

<?phpecho $_cookie["COOKIE"];

Delete

<?php# method One: Set the value to null Setcookie ("cookie", null), #方法二: Set the Expiration Time to Setcookie ("cookie", "value", Time ());

Session

Session is a method of maintaining user session data on the server side, which works as follows:

    1. When the browser first accesses the PHP script, seesion_start () function creates a unique session ID (each client has a unique identity) and automatically saves the session ID to the client Cookie via the HTTP response header. At the same time, a file named after session ID is created on the server side, which is used to save the user's conversational information;

    2. When the same user accesses the site again, the Seesion ID saved in the Cookie is automatically carried over by the HTTP request header Bring it over.

    3. Server PHP script accepted to client request, at this time session_start () function will not assign a new session ID, but in the server's hard disk to find and the session ID of the session file with the same name, this is the user to save the conversation information read out.

First, there are two ways to create a Session's unique identity: by means of a Cookie or GET. PHP uses the Session by default to create a namePHPSESSIDCookie (You can modify the value of the Session.name by php.ini), if the client disables cookies, you can specify that the session ID be passed to the server by a Get method (modify PHP.inisession.use_trans_sidparameters, etc.). Second, the Session is saved in the form of a file. There is a configuration item in the PHP.ini--session.save_path= "", the path that is filled in will save all Session files. The naming format for Session files is:sess_[PHPSESSID的值]。 Each file, which holds the data for a session. Finally, the data stored in the Session file is serialized, such as:

Cityid|i:0;cityname|s:3: "All"; Fanwe_lang|s:5: "ZH-CN"; Fanwe_currency|a:4:{s:2: "id"; s:1: "1"; S:6: "Name_1"; S:9: " RMB "; s:4:" Unit "; S:3:" ¥ "; s:5:" Radio "; S:6:" 1.0000 ";} _FANWE_HASH__|S:32: "77c18770c6cb5d89444c407aaa3e8477";

Session can also be used to save user name, password, personalized settings and other simple information, the following is the session of the use of instructions:

Create

Start Sessionsession_start ();//Register session variable, assign to a user's name $_session["username"] = "Jochen";//Register session variable, assign value to a user's id$_ session["UID"] = 1;
Note: session_start () must be called before the content output of the HTML file

Read

<?phpsession_start (); Echo $_session["username"]; # # Jochenecho $_session["UID"];      # 1

Destroyed

<?phpsession_start (); unset ($_session["username"); unset ($_session["UID"]);

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.