Php Cookie details

Source: Internet
Author: User
Tags php tutorial set cookie setcookie

. What is cookie?

Cookie is a mechanism for storing data on a remote browser to track and identify users. 2. php Tutorial how to set cookie

You can use the setcookie () or setrawcookie () function in php to set the cookie. cookie is a part of the http header. Therefore, setcookie () and other functions must be called before the output data base. This is the same as the header () function. however, you can also use the output buffer function to delay the script output. until all cookies and other http headers are set.
<? Php
$ Value = "something ";
Setcookie ("testcookie", $ value); // Set the cookie value
Setcookie ("testcookie", $ value, time () + 3600); // Set the Expiration time
?>
If you want to set multiple values for a cookie variable, you can add the [] symbol after the cookie variable name.
<? Php
// Set the cookies
Setcookie ("cookie [three]", "cookiethree ");
Setcookie ("cookie [two]", "cookietwo ");
Setcookie ("cookie [one]", "cookieone ");
?>
Setcookie () can also set the domain and directory path of the program that controls the reading of cookies. By default, it can be read only on pages in the same directory structure as the server and within the same level or below, however, if you want to enable/~ from the domain example.com /~ Rasmus directory processing page, and use ssl-based secure connection, you can perform the following settings:
<? Php
$ Value = "something ";
Setcookie ("testcookie", $ value, time () + 3600 ,"/~ Rasmus/"," .example.com ", 1 );
?>
The operation for deleting a cookie is similar to the above. You only need to set the cookie value to null.
<? Php
// Set the expiration date to one hour ago
Setcookie ("testcookie", "", time ()-3600 );
Setcookie ("testcookie", "", time ()-3600 ,"/~ Rasmus/"," .example.com ", 1 );
?> Note:
According to register_globals settings, common php variables can be created from cookies. However, this feature is not recommended because this option is usually disabled for security reasons.
Make sure that no data is output before setting the cookie.
Php automatically receives and processes cookies. You only need to call the cookie directly when you need it. You can use print_r ($ _ cookie) to print out all the values of the cookie array.

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.