PHP and MySQL basic tutorial (3)

Source: Internet
Author: User
Tags php and mysql php3 file

Use cookies to track and identify users

Let's take a look at the content saved in the browser. If you are using ie5, there is a cookie directory under the Windows directory, which contains many text files. The file names are similar to wudong@15seconds%1%.txt, this is the cookie used by the browser to save the value. In earlier versions of IE, the content of cookies can be viewed, but now the content has been encoded. Before the browser obtains a Web page, it first looks at the Domain Name of the page and determines whether it exists in the cookie. If there is a matched cookie, the browser first delivers the matching cookie to the server, then accept the page sent by the processing server.

Here is an example of a cookie application: When I connect to, the browser sends the previously set cookies to Amazon before accepting the first page. Then, checks the transmitted content to see if there is any relevant information in the database. After matching, a custom page is created for me and sent to it.
Assign values to cookies
Prepared by: Yangmei compilation clicks in this article: 127

Cookies must be assigned a value before the server sends any content to the client browser. To do this, the cookie settings must be placed in the

<? PHP

Setcookie ("cookieid", $ userid );






The setcookie function has six parameters separated by commas:

Cookie name, which is a string such as "cookieid ". Colon, comma, and space are not allowed. This parameter is required, and all other parameters are optional. If only this parameter is provided, the cookie will be deleted.
Cookie value, usually a string variable, for example, $ userid. You can also assign a question mark (?) to it ?? To skip the value setting.
The time when the cookie expires. If it is omitted (or assigned to zero value), the cookie will expire after the session ends. This parameter can be an absolute time, represented by DD-mon-yy hh: mm: SS, for example, "24-nov-99 08:26:00 ". It is more commonly used to set a relative time. This is implemented through the time () function or the mktime function. For example, time () + 3600 will invalidate the cookie after one hour.
A path used to match the cookie. When multiple cookie settings with the same name exist on a server, this parameter is required to avoid confusion. The effect of using the "/" path is the same as that of Omitting this parameter. Note that the cookie definition of Netscape places the domain name in front of the path, while PhP is opposite.
The server domain name is also used to match the cookie. Note that a point (.) must be placed before the Domain Name of the server (.). For example, " ". Because the parameter is unacceptable unless more than two vertices exist.
Cookie security level, which is an integer. 1 indicates that the cookie can only be transmitted over a "secure" network. 0 or omitted indicates that any type of network can be used.
Cookies and variables
Prepared by: Yangmei compilation clicks in this article: 127

After the PHP script extracts a cookie from the client browser, it automatically converts it into a variable. For example, a cookie named cookieid becomes the variable $ cookieid.

The content of cookies is reported to exist in the http_cookie_vars array. You can also use this array and cookie name to access the specified cookie value:

Print $ http_cookie_vars [cookieid];
Remember every user
Prepared by: Yangmei compilation clicks in this article: 127

Let's look back at the submitform. php3 file above. It adds the customer's name to the database. Now I want to add something for it. I want to assign a unique user flag to each user and put it in cookies so that every time a user accesses my website, the cookie and the user logo are used, I will be able to know who he is.

MySQL can be set to automatically allocate a number for each new record. This number starts from 1 and is automatically added to 1 each time. With a single SQL statement, you can easily add such a field 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 11 digits. Then, use the "not null" keyword to make the value of this field not null; use "primary key" to set it as an index field, so that the search will be faster. Finally, "auto_increment" defines it as a Field automatically added.

After the user name is inserted into the database, the cookie should be set in their browser. In this case, the value of the USERID field we just talked about is used:

<? 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,

"/");/* The cookie will 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 is executed. In this way, as long as you do not know how to remove the cookies of the browser, the website will always "remember" you.

Read cookie
Prepared by: Yangmei compilation clicks in this article: 127

Let's write a script Like First, the PHP script checks whether the client browser has sent the cookie. If so, the user's name will be displayed. If the cookie is not found, a form is displayed, asking the customer to register their name, add it to the database, and set the cookie in the customer's browser.

First, display the cookie content:

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


That's it. I did not make any judgment in it. I will give it to you to complete it.

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