note 96995 modified in function.setcookie by danbrown
| From: | danbrown@php.net | Date: | Fri, 26 Mar 2010 15:47:02 +0000 |
| Subject: | note 96995 modified in function.setcookie by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-167436@lists.php.net to get a copy of this message | ||
To avoid the browser delay when working with cookies, you can create your cookie during the
initialization process of your web site. If someone visits *any* page of your site, you should
create that cookie with a default value after checking for the default value. Later, when
you're working with the cookie, set it to what you need it to and it will be available on the
next page the user visits.
This helps avoid forcing a browser reload on the client-side. A more refined method of working with
cookies is to use my example above and then interact with the cookies using javascript.
To review:
Perform a conditional check to ensure the cookie does not exist and create it with a controlled
default value. (You can later check for the default value when adding meaningful data)
<?php
$cookie_name = "mytestcookie";
if( !isset($_COOKIE[$cookie_name]) && empty($_COOKIE[$cookie_name]) )
setcookie("$cookie_name", 0, 0, "/");
?>
The cookie "mytestcookie" is now accessibly readable and writeable.
--was--
To avoid the browser delay when working with cookies, you can create your cookie during the
initialization process of your web site. If someone visits *any* page of your site, you should
create that cookie with a default value after checking for the default value. Later, when
you're working with the cookie, set it to what you need it to and it will be available on the
next page the user visits.
This helps avoid forcing a browser reload on the client-side. A more refined method of working with
cookies is to use my example above and then interact with the cookies using javascript.
To review:
Perform a conditional check to ensure the cookie does not exist and create it with a controlled
default value. (You can later check for the default value when adding meaningful data)
<?
$cookie_name = "mytestcookie";
if( !isset($_COOKIE[$cookie_name]) && empty($_COOKIE[$cookie_name]) )
setcookie("$cookie_name", 0, 0, "/");
?>
The cookie "mytestcookie" is now accessibly readable and writeable.
http://php.net/manual/en/function.setcookie.php