PHP setcookie () Usage _php tutorial for assigning a value to a cookie

Source: Internet
Author: User
I wanted to do a feature this morning. After the user logs in, I save the user's name to the cookie but I started using the method of reading the cookie, but no effect, later found that using Setcookie () must take time to be able, let me introduce.

Grammar
Setcookie (Name,value,expire,path,domain,secure)

Parameter description
Name is required. Specifies the name of the cookie.
Value is required. Specifies the value of the cookie.
Expire is optional. Specify the validity period of the cookie.
Path is optional. Specifies the server path of the cookie.
Domain is optional. Specifies the domain name of the cookie.
Secure is optional. Specifies whether to transfer cookies through a secure HTTPS connection.


example, a simple assignment to a cookie

The code is as follows Copy Code

$value = "My cookie value";

Send a cookie that expires in the 24 child
Setcookie ("TestCookie", $value, Time () +3600*24);
?>


Example, a two-dimensional array is stored in a cookie

a.php

The code is as follows Copy Code

$cart _info[0][0] = "1123";
$cart _info[1][0] = "5334521";
$cart _info[1][2] = "DF";
$cart _info[4][2] = "Fefe";

Setcookie ("Xia", Serialize ($cart _info));
?>

b.php

The code is as follows Copy Code

$other = stripslashes ($_cookie[' Xia ');
Print_r (Unserialize ($other));
?>

http://www.bkjia.com/PHPjc/628686.html www.bkjia.com true http://www.bkjia.com/PHPjc/628686.html techarticle I wanted to do a feature this morning. After the user logs in I saved the user's name to a cookie but I started using the method of reading the cookie, but it didn't work, and later found using Setcookie () behind ...

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