PHP Session Control session and cookie Introduction

Source: Internet
Author: User
Tags php session
Background Introduction to Session control generation
Several ways to implement user tracking:
1, the current page variable page
Available on the current page, the variable is released after the page execution is complete.
Pass variable get between 2, two pages
/abc.php?id=12234&age=2 can do user tracking by URL, but it's too tedious.
3. Session Level Sessions
Share your own variables with the same user on the same site.
4, the overall glboal
files, databases. I can use them for anyone, any website. In short, session control is the fact that the server keeps track of successive requests made by the same client.
The difference between a session and a cookie
Cookies: The most important thing is that server-side information to clients is stored by the client. Principle: When a user accesses my server, the Server scripting language sets a cookie to the user (login) according to the business logic, and the user creates the text of the site (usually related to the domain name) under the client computer-specific folder, which is used primarily to record the variables to be saved. Access to multiple sites then create multiple of this file, when the user accesses other pages, the HTTP Association Council through the HTTPD header of the client set of text inside the content to the other pages for use. Multi-personal access is a repeat of the previous step.
The text file that saves the website information is called the cookie file, the whole set of such mechanism is called the cookie mechanism.
In Firefox, the cookie information is not a normal text message and is encrypted. You can view it directly in the browser:
Tools, options, privacy, history, using custom history settings, display cookies
In fact, the use of early cookie is very controversial, in the specified folder, write the specified file and this file must be a text.
Setup method:
1, set the cookie Setcookie (name, value, save time);
Attention:
⑴ If the saved time is not written, the cookie disappears when the browser is closed.
⑵ Cookie uses a hyper-global array of cookies to fetch, (the client has passed the cookie data when the page is accessed again) ⑶ Delete cookie2, Setcookie (name, value, less than the current time); 1 Note: ⑴ setting COOKIE value must be set using Setcookie cannot use _cookie["]=xxoo this way;
⑵setcookie when setting a value the second argument cannot be of type array;
⑶setcookie can be stored in an array, but the array is stored in xxoo[xx] this way. The array can be indexed or associative;
⑷ If an indexed array is used in the Setcookie but you do not give the subscript this format, then the subscript is always 0, and the following value overrides the previous value;
⑸ when you have set up cookies, it is not possible to use the cookie value for the first time if you are on the same page. Because at the same time you request the server side just let the client save;
There is a fourth parameter in the ⑹setcookie, which is the path of the cookie set, and the path to the default cookie is the current directory.
3. Session:
In the upgraded version of the cookie, the cookie takes up space and is better than the computer account leak by reading the website cookie.
Principle: The first time the client logs on to the site according to the business logic will be assigned to the client a non-duplicate ID, this time the server will create a file, the name of the file is assigned to the user's SessionID named, When the client gets SessionID it will be stored in the form of a cookie, and if the next visit page will bring SessionID back in the form of a cookie. When brought over, the server will read the session ID file according to the session mechanism.
4. How to set session
Session_Start (); open session mechanism to open session
Mechanism:
⑴ when the client requests the server for the first time, Session_Start assigns an ID to create the session file.
⑵ If you have already set up a session, it will look for the files in the server based on the session ID brought over by the HTTP protocol.
5, how to take the value, set the value
Session[' Xxoo′]=xxoo; set value _session[' Xxoo ') value also start SESSION start
6. How to destroy the session
⑴session_start ();
⑵ empties the $_session array unset arrays (); Because in the third part, although the file is gone, the value inside is still in memory, it can be used.
⑶ Destroy session file Session_destory ();
7. Delete client session ID
Setcookie (' Session ID value ', ', Time ()-1);
Setcookie (Session_name (), ", Time ()-1);
Attention:
When deleting session ID and Setcookie, it is best to add the fourth parameter
Session_name (); To dynamically obtain the value of SessionID.
The name of the session represented by the Session.name option in php.ini
Session.auto_start this represents the automatic opening session of each page without session_start and is usually turned off.
  • Related Article

    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.