PHP session tracking 1 (41)

Source: Internet
Author: User
Tags set cookie
: This article mainly introduces PHP session tracking 1 (41). If you are interested in the PHP Tutorial, refer to it. PHP session tracking

What is session control?

We need a powerful solution to enable the website to track the interaction between the client and the server, save and remember the identity and information of each user, thus resulting in session control.
?? What is session control:
?? HTTP is a stateless protocol that cannot maintain the connection between two transactions.
?? When a user requests a page and then requests another page, HTTP cannot tell us that the two requests are from the same person.
?? The idea of session control is to track a variable on the website. we can track the variable to support users and display different content and pages based on authorization and user identity.

Session tracking technology

Hide form fields

Add the hidden form fields of the session trace field to the HTML page, but they are not displayed in the client browser.


Specify the names and values used to store session messages in the GET and POST methods.

URL rewriting

?? URL (unified resource positioning) rewriting technology adds a unique session ID to the end of the URL to identify the session.
?? For example, rewrite the following URL to pass the session ID = 1002

Original URL: http://www.myphp.com/load.php: use the example URL to rewrite the URL: http://www.myphp.com/load.php? Id = 1002 Original URL: http://www.myphp.com/bookinfo.php: url:http://www.myphp.com/bookinfo.php? Bookid = 1, 1000

Cookie and session

When we need a wider range of sessions for a longer period of time, we need to use cookies and sessions. For example, you need to log on to a member to download things on some websites. What should we do if we want to know whether the customer has logged on and can perform automatic login? Cookie and session will be known.
?? For example, how does a shopping cart know which products have been selected by customers during online shopping? Cookies and sessions can also be recorded.
?? All in all, cookie and session are technologies that can record the status of customers. even though they are different technologies, session can be done as long as cookies can be done!

Cookie

What is cookie:
?? Cookie is a method in which the server or script can maintain client information under http.
?? Cookies are cookies (a small text file) stored on the web server in the user's browser. they can contain information about users. they are often used to save user names, passwords, and personalized settings, personal preference records. When a user accesses the server, the server can set and access cookie information.
?? The cookie is stored on the client, usually in the temporary cookie folder of IE or Firefox browser. you can delete it manually. Note: If there are too many cookies in the browser that are beyond the permitted scope of the system, the browser will automatically delete them.

Cookie operating principle

When a customer accesses a PHP-based website, the setcookie () function can be used in PHP to generate a cookie. after processing, the system sends the cookie to the client and stores it in C: \ Documents and Settings \ Username \ Cookies directory.
?? Cookie is part of the HTTP header. Therefore, the setcookie () function must be called before any HTML content is sent to the browser. This restriction is the same as the header () function (for details about head () function, refer to it ).
?? When the customer visits the website again, the browser will automatically send the Cookies corresponding to the site under the C: \ Documents and Settings \ User name \ Cookies directory to the server, the server automatically converts the cookie sent from the client into a PHP variable. In PHP5, the cookie sent from the client is converted to a global variable. You can use $ _ COOKIE ['XXX'] to read data.

Define a cookie

Set cookie:
?? Syntax: boolsetcookie (stringname, [stringvalue, [intexpire, [stringpath, [stringdomain, [intsecure]);
This cookie function can have six attributes, commonly used with three parameters.
?? Instance:
$ Value = "the best way is by yourself ";
Setcookie ("cookiename", $ value, time () + 60*60*24*7 );

Setcookie parameter description

Receive and process cookies
PHP has good support for cookies. Like form forms, PHP automatically receives and analyzes HTTP headers from web servers when receiving them. Similar to form receiving, $ _ COOKIE ["cookiename"] or $ HTTP_COOKIE_VARS ["cookiename"] is used for receiving (not recommended)

Note:
If the website has several different file directories that use cookies without paths, you can only access this cookie in the path where the files with cookies are set. If a path is specified, the path used to access the cookie is the specified path.

Create a cookie array:

1:
Setcookie ("CookieArray [0]", "Value 1 ");
Setcookie ("CookieArray [1]", "Value 2 ");
Second:
Setcookie ("CookieArray ['one']", "Value 1 ");
Setcookie ("CookieArray ['two']", "Value 2 ");

Use arrays in setcookie ()

 $ Value) {echo "$ name: $ value
\ N ";}}?>

Delete cookie

There are two ways to delete an existing cookie:
1. call the setcookie with only the name parameter.
The name cookie will be deleted from the client;
Setcookie ("MyCookie"); // delete MyCookie
2. set the Cookie expiration time to time () or time ()-1. note:
Time () reduction does not matter, as long as it is an expiration time, that
Is this Cookie deleted after browsing this page?
(In fact, it is invalid ).
?? For example:
Setcookie ("MyCookie", "Value", time ()-1 );
// Delete the MyCookie.
Note: When a Cookie is deleted, its value is still on the current page.
Valid. If you want to set the cookie to expire after the browser is closed.
You can directly set expiretime to 0, or do not set this value.
For example, setcookie ("name", "value", 0 ).

Cookie considerations

1. there cannot be any html output before setcookie (), that is, space, blank
None of the above. it must be set before html file content output.
?? 2. after setcookie (), you call echo $ _ COOKIE ["name"] on the current page without output. You must refresh or go to the next page to view the cookie value.
• 3. cookie processing is different without a browser. The client can disable cookies, and the browser also limits the number of cookies. A browser can create a maximum of 300 cookies, each of which cannot exceed 4 kB. each WEB site can set a maximum of 20 cookies.
?? 4. cookies are stored on the client. if you disable cookies, your cookies will naturally become useless! Therefore, to avoid over-reliance on cookies, you must first consider the solution if the cookie is disabled.

The above introduces PHP session tracking 1 (41), including some content, and hopes to help friends who are interested in PHP tutorials.

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.