PHP Cookies Guide _php Tutorial

Source: Internet
Author: User
Tags setcookie
Review

A cookie is a way for a server or script to maintain information on a client workstation under the HTTP protocol. A cookie is a small file saved by a Web server on a user's browser that can contain information about a user, such as an identification number, a password, how a user purchases on a Web site, or the number of times a user visits the site. The Web site can access cookie information whenever a user is linked to a server.

How do I set up cookies?

You can use the Setcookie function to set a cookie in PHP. Cookies are part of the HTTP header, so setting the cookie feature must precede any content being sent to the browser. This restriction is the same as the header () function. Any cookie from the client will automatically be converted to a PHP variable. PHP obtains the information header and parses it, extracts the cookie name and changes it into a variable. Therefore, if you set a cookie such as Setcookie ("MyCookie", "cookies") PHP will automatically generate a variable named $mycookie with the value "cookies".

Let's take a look at the Setcookie function syntax:


Init Setcookie (String cookiename,string cookievalue,int cookieexpiretime,path,domain,int secure);

Parameter description:

PATH: Represents a directory on the Web server, default to the directory where the page is called

Domain:cookie the domain name that can be used, which defaults to the domain name of the called page. This domain name must contain two ".", so if you specify your top-level domain, you must use ". MyDomain.com"

Secure: If set to "1", the cookie is only remembered by the server that the user's browser considers to be secure.

Examples of use of cookies

Suppose we have a site that needs to be registered, it automatically recognizes the identity of the user and carries out the relevant actions: if it is a registered user, send him information; if it is not already registered, a link to the registration page is displayed.

In accordance with the above requirements, we first create a database to save the registered user information: First name, last name, email address (visit counter).

Follow the steps below to build the table:


mysql> CREATE database users;
Query OK, 1 row affected (0.06 sec)
mysql> use users;
Database changed
Mysql> CREATE TABLE info (FirstName varchar), LastName varchar (+), e-mail varchar (+), Count varchar (3));
Query OK, 0 rows affected (0.05 sec)
Then build a PHP page to check the cookies against the database.

Since PHP can convert a recognized cookie into a corresponding variable, we can check for a variable named "Mycookies":


......
} else {//If the cookie does not exist
......
}
?>

When a cookie is present, we perform the following steps:

First get the cookie value, use the Explode function to analyze the different variables, add the counter, and set a new cookie:


$info = Explode ("&", $myCookies);
......
$count;
$CookieString = $FirstName. ' & '. $LastName. ' & '. $email. ' & '. $count;
Setcookie ("Mycookies", $CookieString, Time () 3600); Set cookies

The user information is then output using an HTML statement.

Finally, update the database with the new counter value.

If this cookie does not exist, we display a link to the registration page (register.php).

http://www.bkjia.com/PHPjc/631401.html www.bkjia.com true http://www.bkjia.com/PHPjc/631401.html techarticle A summary cookie is a way for a server or script to maintain information on a client workstation under the HTTP protocol. A cookie is a small file saved by a Web server on a user's browser, which can ...

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