How PHP reads Cookies

Source: Internet
Author: User
Tags array insert integer php script php3 file variable domain domain name
Cookie|cookies the cookies in the browser
Let's take a look at what's saved in the browser. If you are using IE5, there is a directory of cookies in the Windows directory, there are a lot of text files, the file name is similar to Wudong@15seconds[1].txt, this is the browser to save the value of cookies. In previous versions of IE, cookies were available for viewing, but the content is now encoded. Before the browser gets a Web page, it will first look at the domain name of the page, whether it exists in a cookie, and if there is a match, the browser will first send the matching cookie to the server before accepting the page that the processing server sends over.
One example of a cookie application: When I connect to Amazon.com, the browser sends the contents of its previously set cookies to Amazon before accepting the first page. Then Amazon.com to the contents of the transfer to check to see if there is no relevant information in the database, after the match, in the creation of a custom page for me to send to come over.
======================
Assign a value to a cookie
You must assign a value to a cookie before the server transmits anything to the client's browser. To do this, the cookie settings must be placed within 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 in the meantime. This parameter is required, and all other parameters are optional. If only this parameter is given, then this cookie will be deleted.
The value of a cookie, usually a string variable, for example: $USERID. Or can you give it a?? To skip the setting of the value.
Time the cookie was invalidated. If omitted (or assigned a value of 0), the cookie will expire at the end of this dialog period (session). This parameter can be an absolute time, expressed in Dd-mon-yy HH:MM:SS, for example: "24-nov-99 08:26:00". And more commonly used is to set a relative time. This is accomplished by the time () function or the Mktime function. For example, time () +3600 will cause the cookie to expire after one hours.
A path to match the cookie. When you have multiple cookies on a server with the same name, you need to use this argument to avoid confusion. The effect of using the "/" path and omitting this parameter is the same. Note that Netscape's cookie definition is to put the domain name in front of the path, and PHP is the opposite.
The domain name of the server, which is also used to match cookies. Note that the server's domain name must be placed before a point (. )。 For example: ". friendshipcenter.com". This argument cannot be accepted unless there are more than two points in existence.
The security level of the cookie, which is an integer. 1 means that the cookie can only be transmitted through a "secure" network. 0 or omitted means that any type of network is OK.
=======
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 becomes a variable $CookieID.
The contents of the cookies are reported to exist in the Http_cookie_vars array, and you can also access the specified cookie value through the array and the name of the cookie:
Print $HTTP _cookie_vars[cookieid];
==============
Remember every user
Looking back over the submitform.php3 file, it's about adding the customer's name to the database, and now I want to add something to it. I want to assign a unique user flag to each user, and then put this logo in the cookie so that whenever the user visits my site, through cookies and the user's logo, I can know who he is.
MySQL can be set to automatically assign a number to each new record, starting with 1 and automatically adding 1 each time. With a row of SQL statements, you can easily add such a field to the datasheet, which I call USERID:
ALTER TABLE dbname
ADD COLUMN
USERID INT (one) not NULL
PRIMARY KEY auto_increment;



We made some special settings for this field. First, it is defined as a 11-bit integer by "INT (11)", and then the value of the field is not null by using the "not NULL" keyword, and then the PRIMARY key is set to the indexed field so that the search is faster; Finally, "Auto_increme NT "Defines it as a field that automatically increases one.
When you insert the user's name into the database, you should set the cookie on their browser. This is the value of the USERID field that we talked about just now:
< PHP
mysql_connect (localhost, username, password);
mysql_select_db (dbname);
mysql_query ("INSERT into TableName (first_name, last_name) VALUES (" $first _name "," $last _name ")");
Setcookie ("Cookieid",
MYSQL_INSERT_ID (),
Time () +94608000,
"/"); * * Three years after the cookie will not expire * *
?>
PHP function mysql_insert_id () returns the value of the field defined by Auto_increment after the last time an insert query was executed. So, as long as you don't clear out the browser Cookies, the site will always "remember" you
==================
Read cookies
Let's write a script like Amazon.com. First, the PHP script checks to see if the client browser sent the cookie, and if so, the user's name will be displayed. If a cookie is not found, a form is displayed that allows the customer to register their name and then adds it to the database and sets the cookie in the customer's browsing.
First, the contents of the cookie are displayed first:
< PHP
Print $CookieID;
?>
Then, you can show the name:
< PHP
mysql_connect (localhost, username, password);
mysql_select_db (dbname);
$selectresult = mysql_query ("SELECT * FROM tablename WHERE USERID =" $CookieID ");
$row = Mysql_fetch_array ($selectresult);
echo "Welcome your presence", $row [first_name], "!";
?>
That's the way it is. I have no judgment in it, and I give it to you to finish it.

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.