How to correctly read Cookies in PHP _ PHP Tutorial

Source: Internet
Author: User
Tags php3 file
Correctly interpret how PHP reads Cookies. What are the problems we are talking about today? Phpsetcookie (CookieID, $ USERID );? The setcookie function for HTMLBODYBODYHTMLPHP to read Cookies has a total of six parameters separated by commas (,). The question we are introducing today is related.

  1. <? Php
  2. Setcookie ("CookieID", $ USERID );
  3. ?>
  4. < HTML>
  5. < BODY>
  6. </BODY>
  7. </HTML>

The setcookie function for PHP to read Cookies 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, ".friendshipcenter.com ". 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 all types of networks can be 117-102 117-202 117-301.

Cookies and variables

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

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.

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:

 
 
  1. <? Php
  2. Mysql_connect (localhost, username, password );
  3. Mysql_select_db (dbname );
  4. Mysql_query ("insert into tablename (first_name, last_name) VALUES (" $ first_name "," $ last_name ")");
  5. Setcookie ("CookieID ",
  6. Mysql_insert_id (),
  7. Time () + 94608000,
  8. "/");/* The cookie will expire after three years */
  9. ?>

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.

PHP reads Cookies

Let's write a script like Amazon.com. 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:

 
 
  1. < ?php
  2. print $CookieID;
  3. ?>

Then, you can display the name:

 
 
  1. <? Php
  2. Mysql_connect (localhost, username, password );
  3. Mysql_select_db (dbname );
  4. $ Selectresult = mysql_query ("SELECT * FROM tablename where userid =" $ CookieID "");
  5. $ Row = mysql_fetch_array ($ selectresult );
  6. Echo "welcome", $ row [first_name], "! ";
  7. ?>

The above is how PHP can read Cookies.


Why? Php setcookie (CookieID, $ USERID );? The setcookie function for html body/html php to read Cookies has six parameters separated by commas :...

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.