A correct interpretation of PHP's method of reading Cookies _php tutorial

Source: Internet
Author: User
Tags php3 file
The question we present to you today is about

    1. < ? PHP
    2. Setcookie ("Cookieid", $USERID);
    3. ?>
    4. < HTML>
    5. < BODY>
    6. < /body >
    7. < /html >

PHP reads the Setcookie function of the cookie in 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-yy HH:MM:SS, for example: "24-nov-99 08: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 you must place a dot 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 means that any type of network can be 117-102 117-202 117-301.

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 line of SQL statements, you can easily add a field like this to a data table, which I call

Userid:

ALTER TABLE dbname
ADD COLUMN
USERID INT (one) not NULL
PRIMARY KEY auto_increment;

We have made some special settings for this field. First, the integer whose type is 11-bit is defined by "INT (11)", then the value of the field is not null with the "NOT NULL" keyword, and the "PRIMARY key" is used to set it as an indexed field, which makes the search faster; Finally, "Auto_increme NT "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:

 
  
  
  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. "/"); /* Cookies will not 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 was executed. This way, as long as you do not clear the browser's Cookies, the site will always "remember" you

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

 
  
  
  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_queryUSERID"$CookieID");
  5. $ Row mysql_fetch_array($selectresult);
  6. echo "Welcome your presence", $row [first_name], "!";
  7. ?>

The above is the implementation of PHP read the specific method of cookies.


http://www.bkjia.com/PHPjc/446055.html www.bkjia.com true http://www.bkjia.com/PHPjc/446055.html techarticle the question we are introducing to you today is about? PHP Setcookie (Cookieid, $USERID);? HTML body/body/html PHP Read the Setcookie function of the cookie a total of six parameters, separated by commas: ...

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