Web Development technology PHP how to read cookies

Source: Internet
Author: User
Keywords Web Development technology PHP how to read cookies
Tags php3 file

   View Cookies on your browser
Let's take a look at what's saved in the browser. If you use IE5, there is a directory of cookies in the Windows directory, there are many text files, the filenames are similar to Wudong@15seconds[1].txt, this is the browser to save the value of the cookies. In previous versions of IE, the contents of the cookies were available for viewing, but now the content has been encoded. Before the browser gets a Web page, it will first look at the domain name of the page, whether it exists in the cookie, if there is a match, the browser will first transfer the matching cookie to the server, and then accept the processing server sent over the page.
Let's start with an example of a cookie application: When I connect to Amazon.com, the browser sends the contents of the previously set cookies to Amazon before accepting the first page. Then amazon.com to check the contents of the transmission, to see if there is any data in the database, after matching, in order to create a custom page for me to come over.
  
   assign a value to cookies
You must assign a value to the cookie before the server transmits anything to the customer's browser. To do this, the cookies must be placed in the < head> tag:
  
< PHP
Setcookie ("Cookieid", $USERID);
?>
< html>
< body>
</body>
The Setcookie function has a total of six parameters, separated by commas:
The name of the cookie, which is a string, for example: "Cookieid". colons, commas, and spaces are not allowed during this period. This parameter is required, and all other parameters are optional. If only this parameter is given, then the cookie will be deleted.
The value of a cookie, usually a string variable, for example: $USERID. You can also assign one to it?? To skip the value setting.
The time the cookie expires. If omitted (or assigned a value of 0), the cookie will expire at the end of the conversation session. This parameter can be an absolute time, expressed in Dd-mon-yy HH:MM:SS, for example: "24-nov-99 08:26:00". The more common is to set a relative time. This is done through the time () function or the Mktime function. For example, time () +3600 will invalidate the cookie after one hours.
A path that is used to match a cookie. This parameter is used to avoid confusion when there is more than one set of cookies with the same name on a server. The effect of using the "/" path and omitting this parameter is the same. Note that Netscape's cookie definition is to place the domain name in front of the path, while PHP is the opposite.
The domain name of the server is also used to match cookies. Note that you must place a dot before the server's domain name (. )。 For example: ". friendshipcenter.com". Because unless there are more than two points in existence, this parameter is not acceptable.
The security level of a cookie is an integer. 1 means that the cookie can only be transmitted through a "secure" network. 0 or omitted indicates that any type of network is possible.
  
   Cookies and Variables
When a PHP script extracts a cookie from the client's browser, it automatically converts it to a variable. For example, a cookie named Cookieid will become a variable $CookieID.
The contents of the cookie are reported to exist in the Http_cookie_vars array, and you can also access the specified cookie value through the name of the array and cookie:
Print $HTTP _cookie_vars[cookieid];
  
   Remember every user
Looking back at the submitform.php3 file above, it's the role of adding the customer's name to the database, and now I want to add something to it. I want to assign a unique user logo to each user, and then put this flag in the cookie so that whenever a user visits my website, through a cookie and the user's logo, I can know who he is.
MySQL (the best combination with PHP) can be set to automatically assign a number to each new record, which starts at 1 and automatically adds 1 each time. With a single row of SQL statements, you can easily add such a field to the data table, which I call the USERID:
ALTER TABLE dbname
ADD COLUMN
USERID INT (one) not NULL
Prima (the most perfect virtual host management system) RY KEY auto_increment;

12 Next Page
  • 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.