PHP Set cookie retention time for browser process

Source: Internet
Author: User
Tags set cookie setcookie

For example:

The following are the referenced contents:

<?php
$USERID="PHPer";
$CookieTime=0;
setcookie("USERID", "$USERID", time()+$CookieTime,"/","pcpchina.com");
?>

You will find that after this statement is executed, there is nothing in the cookie, and the next page shows no $userid the cookie variable value.

The analysis of the problem is as follows:

First, setting the "Cookie Expiration Time" value for the browser process is not the current UNIX timestamp +0, and if it is set to the browser process, the expiration time is set to 0 directly.

Second, it is not clear you test this page when the domain name is used, if you set the "pcpchina.com", it means that you must use "pcpchina.com" to access cookies will be effective, in fact, if your domain name a lot of access to this page, So this place can be empty or the domain name that accesses this cookie is below a domain, then set to ". Pcpchina.com", Remember "dot" in front.

The above procedure, if you write this, may be effective:

The following are the referenced contents:

<?php
$USERID="PHPer";
$CookieTime=0;
setcookie("USERID","$USERID",0,"/","");
echo(isset($_COOKIE['USERID'])?$_COOKIE['USERID']:'');
?>

The first time you open this page, there will be no output, because the cookie does not take effect immediately on the current page.

will be displayed after the refresh.

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.