SoFunction
Updated on 2025-04-09

Basic tutorials for PHP and MySQL (III)

Use cookies to track and identify users

Let's take a look at what's saved in the browser. If you are using IE5, there is a cookies directory in the windows directory, which contains many text files, and the file names are similar to wudong@15seconds[1].txt, which is the cookies used by the browser to save values. In previous IE versions, the content of cookies was visible, 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 and whether it exists in the cookie. If there is a matching cookie, the browser will first transmit the matching cookie to the server, and then accept the page sent by the processing server.

Let me give you an example of a cookies application: When I connect to , the browser will send the content of the cookies it previously set to Amazon before accepting the first page. Then check the transferred content to see if there is any relevant information in the database. After matching, a customized page is created for me to be transferred to it.
Assign values ​​to cookies
Author: Yangmei Compiled Number of clicks on this article: 127

Cookies must be assigned values ​​before the server delivers anything to the client's browser. To do this, the cookies settings must be placed in the <HEAD> tag:

< ?php

setcookie("CookieID", $USERID);

?>

< HTML>

< BODY>

< /BODY>

< /HTML>

The setcookie function has a total of six parameters, separated by commas:

The name of the cookie is a string, for example: "CookieID". Colons, commas and spaces are not allowed. This parameter is required, while all other parameters are optional. If only this parameter is given, then the cookie will be deleted.
The value of a cookie is usually a string variable, such as: $USERID. You can also assign a ?? to skip the value setting.
The time when the cookie expires. If omitted (or assigned to zero), the cookie will expire after the end of this conversation period (session). This parameter can be an absolute time, expressed as DD-Mon-YY HH:MM:SS, for example: "24-Nov-99 08:26:00". What is more commonly used is to set a relative time. This is achieved through the time() function or the mktime function. For example, time()+3600 will make the cookie invalid after one hour.
A path to match cookies. When there are multiple cookies with the same name on a server, this parameter must be used to avoid confusion. The effect of using the "/" path is the same as omitting this parameter. It should be noted that Netscape's cookie definition puts the domain name before the path, while PHP is the opposite.
The server's domain name is also used to match cookies. It should be noted that a dot ( . ) must be placed in front of the server's domain name. For example: "." . Because this parameter cannot be accepted unless there are more than two points.
The security level of a cookie is an integer. 1 means that this cookie can only be transmitted through a "secure" network. 0 or omitted means that any type of network is OK.
Cookies and variables
Author: Yangmei Compiled Number of clicks on this article: 127

When the 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 to be in the HTTP_COOKIE_VARS array. You can also access the specified cookie value through this array and the name of the cookie:

print $HTTP_COOKIE_VARS[CookieID];
Remember every user
Author: Yangmei Compiled Number of clicks on this article: 127

Looking back at the submitform.php3 file above, its purpose is to add 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 put this logo in cookies so that whenever a user visits my website, through the cookies and the user logo in it, I can know who he is.

MySQL can be set to automatically assign a number for each new record, which starts from 1 and will automatically add 1 each time. With a single line of SQL statements, you can easily add a field like this to the data table. I call it USERID:

ALTER TABLE dbname

ADD COLUMN

USERID INT(11) NOT NULL

PRIMARY KEY AUTO_INCREMENT;

We have made some special settings for this field. First, use "INT(11)" to define an integer of its type as 11 bits; then use the "NOT NULL" keyword to make the value of this field not NULL; then use "PRIMARY KEY" to set it as an index field, so that the search will be faster; finally, "AUTO_INCREMENT" defines it as an automatic incremental field.

When inserting the user's name into the database, you should set cookies on their browser. What we are using at this time 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 not expire after three years */

?>

The PHP function mysql_insert_id() returns the value of the field defined by AUTO_INCREMENT after the last INSERT query was executed. In this way, as long as you don’t clear the browser’s cookies, the website will always “remember” you

Read cookies
Author: Yangmei Compiled Number of clicks on this article: 127

Let's write a script like the one I did. First, the PHP script will check whether the client browser has sent a cookie. If that is the case, the user's name will be displayed. If a cookie is not found, a form is displayed, and the customer has their name registered, then added it to the database, and the customer browses it to set the cookie.

First, let’s display 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], "!";

?>

That's it. I didn't make any judgments in it, I left it to you to complete it myself