Use cookies to track identification users
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 of a cookies application first: When I connect to Amazon.com, the browser will transmit the contents of the cookies that they previously set to Amazon before accepting the first page. Then Amazon.com checks the content transferred. See if there is any related information in the database, after the match, it is transferred to the custom page for me.
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:PHP
SetCookie ("CookieID", $ UserID;
? >
Body>
Html>
SetCookie functions have six parameters, separated by commas:
The name of the cookie is a string, such as "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 the cookie is usually a string variable, such as $ userid. Can you assign it for it? ? To slightly value settings.
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 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 is 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 author: Yang eyebrows compile this article Click: 127
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 be a number of automatic allocation of each new record. This number starts from 1, and you will automatically add 1. With a line SQL statement, you can easily add such a field to the data sheet, I put It is called 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:
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,
"/"); / * 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 cookie
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:
PHP
Print $ cookieid;
? >
Then you can display 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 to you", $ row [first_name], "!";
? >
This is the case. I didn't make judgments in it, gave you yourself to do it.