Cookie usage in PHP

Source: Internet
Author: User
Tags cookie names session id set cookie setcookie valid

Cookies and sessions can temporarily save variables used in multiple pages, but they are fundamentally different.

Cookies are stored in the client browser and the session is saved on the server. The connection between them is that the session ID is generally stored in a cookie.

How Cookies work

When a customer visits a Web site, you can use the Setcookie function in PHP to generate a cookie, which is processed to send the cookie to the client and save in the C:documents and Settings username cookies

Table of Contents. Cookies are part of the HTTP header, so the Setcookie function must be invoked before any content is sent to the browser. This limit is the same as the header () function. When the customer accesses the site again, the browser

Automatically sends a cookie from the c:documents and settings user name cookies to the server, and the server converts the cookie from the client to a PHP variable automatically.

Read the COOKIE from the client by $_cookie[' xxx '.

Creating cookies

The Setcookie () function is used to create a cookie that returns TRUE successfully, otherwise it returns FALSE.


BOOL Setcookie (string name [, string value [, int expire [, String path [, String domain]]]

Parameter description:

Name Cookie Names

Value optional, cookie values

Expire optional, expiration time, timestamp format

Path optional, server-side valid path,/means the entire domain name is valid, and defaults to the path of the page when the cookie is currently set

Domain optional, the cookie is valid


Setcookie ("username", "Nostop", Time () +3600); A cookie named username is set with a value of Xiaoli and expires after 1 hours. If the time is omitted, the cookie will expire after the session (typically the browser closes).


Cookies are stored in a certain format in a specific location on the user's computer.


Setcookie ("username", "Nostop", Time () +3600, "/", "."); The cookie is valid under the/path of the domain name (that is, the whole station is valid).


Read cookies

PHP has a built-in $_cookie variable to access the set COOKIE value.



echo $_cookie["username"]; Nostop


Destroying cookies

You can destroy a cookie by setting the cookie expiration time to a previous point in time:


Setcookie ("username", "" ", Time ()-3600);


Cookies considerations

1, Setcookie () can not have any HTML output, is a space, blank lines are not.

2, Setcookie (), you call echo $_cookie["name" on the current page, there is no output. You must refresh or go to the next page before it expires before you can see the cookie value.

3, because the cookie information stored in the user's computer, then it is possible to counterfeit or modify cookies to create a cookie spoofing, you can generally encrypt the value of cookies to prevent cheating. When you read the cookie, you can decrypt the cookie.

4, cookies are stored in the client, the user has disabled cookies, your cookie is not a natural effect!


Setcookie (' username ', ' nostop ', Time () +3600); Creating cookies

if (Isset ($_cookie["username")) {//Use the Isset () function to detect whether the COOKIE variable has been set

echo "Hello!"    ". $_cookie[" username "]; Hello! Nostop Read Cookies


echo "Please login";



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.