How do PHP read cookies

xiaoxiao2021-03-06  40

Look at the browser cookies

Let's take a look at the content saved in the browser. If you use IE5, there is a cookies directory in the Windows directory, there are a lot of text files, the file name is similar to wudong @ 15seconds [1] .txt, this is the cookies for the browser to save the value. . In the previous IE version, the content of cookies can be viewed, but the current content has been encoded. Before the browser gets a web page, it will look at the domain name of this page. Do you exist in cookies, if there is a match, the browser will first transfer the matching cookie to the server, and then accept the processing server to transfer Page.

Example for first, a cookies application: When I connect to Amazon.com, the browser will transmit the contents of the cookies before accepting the first page to Amazon. Then Amazon.com is checked for the transferred content to see if there is related information in the database. After matching, it is transferred to I create a customized page.

======================

Assign the value for cookies

You must assign a cookies before the server is transmitted to the client browser. To do this, the setting of the cookies must be placed in the tag:

SetCookie ("CookieID", $ UserID;

?>

SetCookie functions have six parameters, separated by commas:

The name of the cookie is a string, for example: "cookieid". There is no colon, comma, and spaces. This parameter is necessary, while all other parameters are optional. If only this parameter is given, then this cookie will be deleted.

The value of cookie is usually a string variable, for example: $ userid. It is also possible to give a set of settings for it.

Cookie invalidates. If it is omitted (or being assigned zero), the cookie will fail after the session period (session). This parameter can be an absolute time, represented by DD-MON-YY HH: mm: SS, such as "24-NOV-99 08:26:00". More commonly used is to set a relative time. This is implemented by a Time () function or MKTIME function. For example, Time () 3600 will make cookies to fail after an hour.

A path used to match cookies. This parameter is used to avoid confusion on a server. The effect of using the "/" path and omit 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 opposite to it.

The domain name of the server is also used to match cookies. It should be noted that you must put a point before the server's domain name (.). For example: ".friendshipCenter.com". Since there are more than two points exist, this parameter cannot be accepted. Cookie's security level is an integer. 1 means that this cookie can only be transmitted through the "security" network. 0 or omitted, any type of network can be indicated.

=======

Cookies and variables

When a PHP script extracts a cookie from the client browser, it will automatically convert it into a variable. For example: a cookie named cookieid will become a variable $ cookieid.

The content of cookies is reported in an HTTP_COOKIE_VARS array, and you can access the specified cookie value through this array and cookie name:

Print $ http_cookie_vars [cookieID];

==============

Remember every user

Going back to see the SubmitForm.php3 file above, its role is to add the name of the customer to the database, now I want to add something for it. I want to allocate a unique user logo for each user, then put this flag in cookies, so whenever the user visits my website, I can know who he is by cookie and the user flag. .

MySQL can be set to a number of automatic allocation of each new record, and this number starts from 1, and each automatically adds 1. With a line SQL statement, you can easily add such a field to the data sheet, and I call it UserId:

Alter Table DBName

Add Column

Userid Int (11) Not NULL

PRIMARY Key Auto_Increment;

We made some special settings for this field. First, "int (11)" defines an integer of 11-bit; then use the "not null" keyword to make the value of this field cannot be null; use "primary key" to set it to index fields, so Search will be faster; Finally, "Auto_Increment" defines it to automatically add a field.

Once the user's name is inserted into the database, you should set up a cookie on their browser. At this time, it is the value of the userid field we just talked about:

MySQL_Connect (localhost, username, password);

MySQL_SELECT_DB (DBNAME);

MySQL_QUERY ("INSERT INTO TABLENAME (" $ first_name "," $ last_name ")")

SetCookie ("cookieid",

MySQL_INSERT_ID (),

TIME () 94608000,

"/"); / * Cookies will fail after three years * /

?>

PHP function mysql_insert_id () Returns the value of the field defined by auto_increment after the last time the INSERT query is executed. In this way, as long as you don't clear the cookies of the browser, the website will always "remember" you.

================== read cookies

Let's write a script like Amazon.com. First, the PHP script will check if the client browser sends a cookie. If so, the user's name will be displayed. If you don't find the cookie, you will display a form, let the customer register their name, then add him to the database and browse it in the customer.

First, let's show the content of the cookie:

Print $ cookieid;

?>

Then you can display the name:

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 to you", $ row [first_name], "!";

?>

This is the case. I didn't make judgments in it, gave you yourself to do it.

Excerpted from:

Source Author: tree2000


New Post(0)