PHP session Tracking One (41)

Source: Internet
Author: User
Tags session id php session setcookie

PHP Session TrackingWhat is session control

We need a powerful solution that allows the Web site to track the interaction between the client and the server, saving and remembering each user's identity and information, thus creating session control.
?? What is session control:
?? HTTP is a stateless protocol that cannot be maintained to maintain a connection between two transactions.
?? When a user requests a page and then requests another page, HTTP cannot tell us that the two requests came from the same person.
?? The idea of session control is to be able to track a variable in the Web site, we can track the variables, we can do the user support, and according to the authorization and user identity display different content, different pages.

Session Tracking Technology

Hide form fields

Adds a hidden form field for a session tracking field to an HTML page, but does not appear in the client browser

<form action = "main.php" method= "POST" ><input type = "hidden" name= "Checkid" value= "zy7k" ></form>
In the Get and post methods, specify the name and value to store for the session message.
URL rewriting

?? URL (Uniform resource positioning) Rewrite technology adds a unique session ID to the end of the URL to identify the session.
?? For example, rewrite the following URL delivery session id=1002

Original url:http://www.myphp.com/load.php url:http with added parameters://  www.myphp.com/load.php? id=1002 Original URL:http://www.myphp.com/bookinfo.php rewritten with added parameters Url:http://www.myphp.com/ bookinfo.php?bookid=1000
Cookies and session

We need to use cookies and sessions when we need conversations that can be tracked more broadly and for longer periods of time. For example, in some sites to download things need members to first login. We want to know if the customer has landed, and can be automatic landing how to do? The cookie and the session can be known.
?? such as online shopping, shopping cart How to know what customers have selected products? Cookies and sessions can also be recorded.
?? All in all, cookies and sessions are technologies that record customer status, even though they are different technologies, but as long as cookies are available, the session can do the same!

Cookies

What is a cookie:
?? A cookie is a way for a server or script to maintain client information under the HTTP protocol.
?? A cookie is a cookies (a small text file) that a Web server holds on a user's browser and can contain information about a user, often used to save usernames, passwords, personalization settings, personal preferences, and so on. When a user accesses a server, the server can set and access cookie information.
?? Cookies are stored on the client, usually in the cookie Temp folder of IE or Firefox browser, and can be deleted manually. Note: If there is too much cookie on the browser, the browser will automatically delete it if it exceeds the range allowed by the system.

How Cookies work

When a customer accesses a Web site based on a PHP technology, a cookie can be generated in PHP using the Setcookie () function, which is processed to send the cookie to the client and saved in C:\Documents and Settings\ username \ The Cookies directory.
?? A cookie is part of the HTTP header, so the Setcookie () function must be called before any content in the HTML itself is sent to the browser. This restriction is the same as the header () function (see the head () function for your own reference).
?? When the customer visits the website again, the browser automatically sends the cookie corresponding to the site in the C:\Documents and Settings\ user name \cookies directory to the server, and the server will automatically convert the cookie from the client to a PHP variable. In PHP5, the cookie sent by the client is converted to a global variable. You can read it by $_cookie[' xxx '.

Define a cookie

Set Cookies:
?? Syntax: Boolsetcookie (Stringname,[stringvalue,[intexpire,[stringpath,[stringdomain,[intsecure]]]);
This cookie function can have 6 properties, which are commonly used with 3 parameters.
?? Instance:
$value = "The best-of-the-yourself";
Setcookie ("CookieName", $value, Time () +60*60*24*7);

Setcookie parameter explanation

Receiving and processing cookies
PHP has good support for cookies and, like form forms, PHP automatically receives HTTP headers from the Web server and parses it when it is received. When receiving the same as the form receive, use $_cookie["CookieName"] or $http_cookie_vars["CookieName"] (not advocated)

Attention:
If a Web site has several different file directories, using a cookie without a path, the cookie can only be accessed under the path of the file where the cookie is set. If you specify a path, follow the path you set as the specified path to access the cookie.

To create an array of cookies:

One
Setcookie ("cookiearray[0]", "Value 1");
Setcookie ("cookiearray[1]", "Value 2");
Second:
Setcookie ("cookiearray[' One '", "Value 1");
Setcookie ("cookiearray['", "" Value 2 ");

Using arrays in Setcookie ()

<?Phpsetcookie ("Cookie[three]","Cookiethree"); Setcookie ("Cookie[two]","Cookietwo"); Setcookie ("Cookie[one]","Cookieone");//after the page is refreshed, it appearsif(Isset ($_cookie['Cookies'])) {foreach($_cookie['Cookies'] as$name =$value) {echo"$name: $value <br/>\n";}}?>
Delete Cookies

There are two ways to delete an already existing cookie:
1. Call the Setcookie with the name parameter only, then named this
The cookie for name will be deleted from the client;
Setcookie ("MyCookie"); Delete MyCookie
2. Set the cookie to expire at time () or hour ()-1, note:
Time () minus how much does not matter, as long as the expiration of the line, that
This cookie was deleted after the page was browsed.
(In fact, it is invalid).
?? For example:
Setcookie ("MyCookie", "Value", Time ()-1);
Delete MyCookie.
Note: When a cookie is deleted, its value is still in the current page
Effective. If you want to set the cookie to expire after the browser is closed.
You can set Expiretime to 0 directly or not.
For example: Setcookie ("name", "value", 0).

Cookie Precautions

1, Setcookie () can not have any HTML output, is a space, blank
No, you must set the HTML file before the content output.
?? 2, Setcookie (), you call echo $_cookie["name" on the current page there will be no output. You must refresh or go to the next page to see the cookie value.
• 3. Do not use the browser to treat different cookies. The client can disable cookies and the browser will limit the number of cookies. A browser can create a maximum of 300 cookies, and each cannot exceed 4KB, and each Web site can set a total of no more than 20 cookies.
?? 4, the cookie is kept on the client, the user has disabled the cookie, your cookie naturally also has no effect! Therefore, to avoid over-reliance on cookies, first think about the solution if the cookie is disabled in case

PHP session Tracking One (41)

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.