Use cookies to track and identify users in PHP

Source: Internet
Author: User
Tags php tutorial php3 file setcookie
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 cookie setting must be placed in theInside the tag:
   Setcookie ("Cookieid", $USERID);
?>
  
  
  
  
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-yyhh:mm:ss, for example: "24-nov-9908: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 a dot (.) must be placed 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 can be set to automatically assign a number to each new record, which starts from 1 and automatically adds 1 each time later. With a single row of SQL statements, you can easily add such a field to the data table, which I call the UserID:
Altertabledbname
AddColumn
Useridint (one) notnull
Primarykeyauto_increment;

We have made some special settings for this field. First, it is defined by "INT (11)" As an integer with a 11-bit type, and then the "notnull" keyword makes the value of the field not NULL, and then "PRIMARYKEY" sets it as an indexed field, which makes the search faster; Finally, "auto_ INCREMENT "Defines a field that is automatically added to one.

When you insert a user's name into the database, you should set a cookie on their browser. This is the value of the UserID field we talked about just now:

   Mysql_connect (Localhost,username,password);
mysql_select_db (dbname);
mysql_query ("Insertintotablename (First_name,last_name)
VALUES (' $first _name ', ' $last _name ')
");
Setcookie ("Cookieid",
MYSQL_INSERT_ID (),
Time () +94608000,
*/* The cookie 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. This way, as long as you do not clear the browser's cookies, the site will always "remember" you

Read cookies

Let's write a script like Amazon.com. First, the PHP script checks whether the client's browser has sent a cookie, and if so, the user's name will be displayed. If the cookie is not found, a form is displayed, the customer registers their name, then adds it to the database, and sets the cookie in the client's browser.

First, let's show the contents of the cookie:
   Print$cookieid;
?>
Then, you can display the name:
   Mysql_connect (Localhost,username,password);
mysql_select_db (dbname);
$selectresult =mysql_query ("Select*fromtablename
Whereuserid= ' $CookieID '
");
$row =mysql_fetch_array ($selectresult);
echo "Welcome your presence", $row [first_name], "!";
?>
That's the way it is. I didn't make a judgment in it, so you can do it yourself.


The above describes the use of cookies in PHP to track and identify users, including aspects of the content, I hope that the PHP tutorial interested in a friend to help.

  • 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.