Introduction to php session control sessions and cookies

Source: Internet
Author: User
Php session control SESSION and COOKIE introduction background of SESSION control
Several methods for implementing user tracking:
1. variable page of the current page
The current page is available. after the page is executed, the variable is released.
2. pass the get variable between two pages
Pass the value through URL/abc. php? Id = 12234 & age = 2 can be used for user tracking, but it is too cumbersome.
3. session-level session
The same user shares his/her own variables on the same website.
4. global glboal
Files and databases. Anyone or any website can use them. In short, session control means that the server can track the continuous requests made by the same client.
Difference between SESSION and COOKIE
Cookie: information sent from the server to the client is stored on the client. Principle: when a user accesses my server, the server scripting language sets the cookie for the user (login) according to the business logic ), the user creates the text of the website (usually related to the domain name) in a folder specified by the client computer. this text is mainly used to record the variables to be saved, to access multiple websites, create multiple files, when you access other pages, the http protocol uses the httpd header to bring the content in the text set by the client to other pages for use. Multiple visits are repeated in the previous step.
The text file that Stores website information is called a cookie file. a complete set of such mechanisms are called the cookie mechanism.
In Firefox, cookie information is not plain text information and is encrypted. You can view it in a browser:
Tools-> Options-> Privacy-> History-> use custom History Settings-> Display cookies
In fact, the use of early cookies is controversial. in a specified folder, write the specified file and the file must be a text file.
Setting method:
1. set the cookie setcookie (name, value, and storage time );
Note:
(1) if the storage time is not written, the cookie disappears when the browser is closed.
(2) use the ultra-global array cookie to retrieve the COOKIE (when the page is accessed again, the client has passed the cookie data) (3) delete cookie2 and setcookie (name, value, less than the current time); time ()? 1. note: (1) you must use setCookie to set the cookie value and cannot use _ COOKIE ["] = xxoo;
(2) when setcookie is set, the second parameter cannot be of the array type;
(3) the setcookie can store arrays, but the array must be stored in xxoo [xx. Arrays can be indexed or associated;
(4) If the index array is used in setcookie but you do not give this format to the lower mark, the subscript will always be 0, and the subsequent values will overwrite the previous values;
When you set the cookie, if you use the cookie value immediately on the same page, it is unavailable for the first time. Because the server just saves the client while you request it;
The fourth parameter in the secure setcookie is the path set by the cookie. the default cookie path is in the current directory.
3. session:
If the cookie is upgraded, the cookie will occupy space and the computer account may be leaked by reading the website COOKIE.
Principle: when the client logs on to the website for the first time, a unique ID is assigned to the client based on the business logic. at this time, the server creates a file, this file is named after the sessionid of the assigned user. after the client obtains the sessionid, it will store it as a cookie, if you access the page again next time, the sessionid will be carried in the form of a cookie. The server reads the session id file based on the session mechanism.
4. how to set a session
Session_start (); enable session
Mechanism:
(1) when the client requests the server for the first time, session_start will assign an ID to create the session file.
(2) If you have already set a session, it will find the file in the server based on the session id brought in by the HTTP protocol.
5. how to get and set values
SESSION ['xxoo '] = xxoo; set the value of _ SESSION ['xxoo'] to enable session start first.
6. how to destroy a session
(1) session_start ();
(2) clear the $ _ SESSION array unset array (); // because the value in the third part is still in the memory even though the file is missing.
(3) destroy the session file session_destory ();
7. delete the client session ID
Setcookie ('session ID value', ", time ()-1 );
Setcookie (session_name (), ", time ()-1 );
Note:
When deleting session id and setcookie, it is best to add the fourth parameter
Session_name (); to dynamically obtain the value of sessionID.
The session. name option in php. ini indicates the name of the session.
Session. auto_start indicates that the session is automatically enabled when you enter every page without session_start.

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.