★ wanayoo — archive 1999 http://www.php.net/manual/function.setcookie.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to HTTP
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
HTTP
* header
* header_sent
* setcookie
Manual: setcookie
View the source code for this pageSearch the site



Previous page
 header_sent
 Updated
Sat, 12 Aug 2000
Hyperwave 
Next page


setcookie

(PHP3 , PHP4 )

setcookie -- Send a cookie

Description

int setcookie (string name, string value, int expire, string path, string domain, int secure)

setcookie() defines a cookie to be sent along with the rest of the header information. Cookies must be sent before any other headers are sent (this is a restriction of cookies, not PHP). This requires you to place calls to this function before any <html> or <head> tags.

All the arguments except the name argument are optional. If only the name argument is present, the cookie by that name will be deleted from the remote client. You may also replace any argument with an empty string ("") in order to skip that argument. The expire and secure arguments are integers and cannot be skipped with an empty string. Use a zero (0) instead. The expire argument is a regular Unix time integer as returned by the time() or mktime() functions. The secure indicates that the cookie should only be transmitted over a secure HTTPS connection.

Common Pitfalls:

Cookies will not become visible until the next loading of a page that the cookie should be visible for.

In PHP3, multiple calls to setcookie() in the same script will be performed in reverse order. If you are trying to delete one cookie before inserting another you should put the insert before the delete. In PHP4, multiple calls to setcookie() are performed in the order called.

Some examples follow:

Example 1. setcookie() examples


setcookie ("TestCookie", "Test Value");
setcookie ("TestCookie", $value,time()+3600);  /* expire in 1 hour */
setcookie ("TestCookie", $value,time()+3600, "/~rasmus/", ".utoronto.ca", 1);
      

Note that the value portion of the cookie will automatically be urlencoded when you send the cookie, and when it is received, it is automatically decoded and assigned to a variable by the same name as the cookie name. To see the contents of our test cookie in a script, simply use one of the following examples:


echo $TestCookie;
echo $HTTP_COOKIE_VARS["TestCookie"];
      

You may also set array cookies by using array notation in the cookie name. This has the effect of setting as many cookies as you have array elements, but when the cookie is received by your script, the values are all placed in an array with the cookie's name:


setcookie ("cookie[three]", "cookiethree");
setcookie ("cookie[two]", "cookietwo");
setcookie ("cookie[one]", "cookieone");
if (isset ($cookie)) {
    while (list ($name, $value) = each ($cookie)) {
        echo "$name == $value<br>\n";
    }
}
      

For more information on cookies, see Netscape's cookie specification at http://www.netscape.com/newsref/std/cookie_spec.html.

Microsoft Internet Explorer 4 with Service Pack 1 applied does not correctly deal with cookies that have their path parameter set.

Netscape Communicator 4.05 and Microsoft Internet Explorer 3.x appear to handle cookies incorrectly when the path and time are not set.


User Contributed Notes: setcookie


seoman@geocities.com
29-Jul-1998 03:03
SetCookie()'s behavior is dependant on where it is called within the web document tree. For example, setting a cookie from a php script in a directory makes that cookie available only to other scripts in that directory and its subdirectories. However, setting a cookie of the same name from a script in the directory's parent directory will override all cookies of that name in the parent directory's subdirectories. To better control the behavior of cookies, you might try sending a raw Set-Cookie: header with the header() function (see Netscape's cookie spec for information on the Set-Cookie: header).


bside@togdog.com
16-Nov-1998 08:41
Don't try to setcookies and then do a header("Location: xxx") in the same go. IE4 (at least) sees the location field (which is ALWAYS before the cookie setting headers) and goes for it, never setting the cookies(?).


whit@transpect.com
02-Dec-1998 12:51
Not sure if this is setcookie's output, or Netscape's reception of it, but if the cookie _name_ contains a character such as "/" it goes through literally, whereas if the cookie _value_ contains a "/", it gets url-encoded - which means when you fetch it back, if you don't decode it, it won't match what your program's looking for (in my case, a module other than PHP is checking the cookie). IMHO, would be more useful if it skipped encoding the cookie value, just as with the name.


vic@zymsys.com
07-Dec-1998 04:31
I just found something I thought was odd. I tried to do a setcookie("unix-socket-faq-name",...) and couldn't read the values back. They were in my cookie file, but the expected $unix-socket-faq-name variable didn't come back. It appears that dashes are not allowed in variable names.


gerhard@frappe.iaf.nl
10-Jan-1999 03:58
Just to complete the info on IIS and it's weird cookie behaviour:



There is a an article in their knowleggbase on the subject with Article ID: Q176113

the solution is to add nph- in front of the CGI name. This also works for PHP pages.







twyst@twysted.net
14-Jan-1999 01:15
There IS a solution to the previously mentioned IE4 setcookie/header("Location:xxx") problem. What you do is this:

Place the following into a file, such as "redir.php3":


header("Location: $redir_to\n\n");


Then, in the file that is setting the cookies, place this:


$redir_to = "http://your_url_here";

include("redir.php3");


That'll get around the pesky IE4 problem. =)



bazyar@hypermall.com
18-Mar-1999 02:31
With php 3.0.5, and IE 4.01, setcookie followed by a Header("Location:") works fine as long as the Location is a <b>local</b> redirect. I.e.,

<pre>
Header("Location: http://www.foobar.com")
</pre>

won't work, but
<pre>
Header("Location: /index.html")
</pre>
<i>will</i>.



amp26@drexel.edu
16-Apr-1999 02:46
a few notes:

MSIE v3.*: same problem as noted above for netscape. will not accept cookies unless there is a time and path set. domain can be null it seems.

MSIE v4.*: ive been using Location and Set-Cookies within the same code and both 4.* and 3.* process both correctly. it may only work for local addresses, but it works for local addresses even if they have the full "http://the.same.computer/file.html" expanded url.

Cookies can not be read in the same page they are set in because PHP parses cookies when the page loades. if you need to use the cookie variable in the same page, manually set a variable $cookie_name = $what_you_used_in_SetCookie.



mdean@kcnet.com
23-Apr-1999 11:46
After experiencing the infamous IE problem at work with our installation of Double Choco Latte (no code posted yet, but the curious can visit http://dcl.netpedia.net), I felt compelled to share my findings here.

I run DCL on a Linux box in my cube. This box is attached to what is a predominantly Windows 9x/NT network. Last week, everything was working fine for Navigator <em>and</em> IE. But, something happened (I believe in a sysadmin way) to the network which caused (strangely enough) all the IE clients to stop working, but Navigator was still OK.

So, after searching high and low, I finally got a working solution in place. My solution involves setting the cookies and doing the redirect within META tags. I have a login.php3 script which is included at the beginning of all other scripts to be sure the user is valid every time an action is performed. login.php3 used to use the SetCookie function and do a header("Location: index.html") for the redirect.

New way: META HTTP-EQUIV Set-Cookie and META HTTP-EQUIV Refresh. This is sloppier than the code that <em>did</em> work, but now IE and Navigator both function once again!



daevid@TheMatrix.com
29-Apr-1999 08:20
Just an observation that maybe everyone already knows, but I find it quite annoying.
It seems that if I store a cooke for $test_id=5 for example, and call the page from somewhere else passing in a new value, such as:
<a href=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2F%26quot%3Bmypage.phtml%3Ftest_id%3D100%26quot%3B%26gt%3B%26lt%3B%2Fa%26gt%3B&y=1999
then when mypage.phtml loads and I "echo $test_id", it will give me "5" not 100.

I don't know if this is a "cookie spec" thing or a PHP thing, but it seems to me I should get 100.
that is, if I specifically sent in a value to that page, then I clearly wish to use that value instead of the cookie's value...



dante@mpath.com
27-May-1999 08:18
For a quick and dirty way to test Cookie support in a browser, here is
a script that will reload itself and check the status of the cookie. It works
for me. Your mileage may vary.
<pre>
&lt;?php
$status = 0;
if (isset($myTstCky) && ($myTstCky == "ChocChip")) $status = 1;
if (!isset($CCHK)) {
setcookie("myTstCky", "ChocChip");
header("Location: $PHP_SELF?CCHK=1");
exit;
}
?&gt;
&lt;html&gt;
&lt;head&gt;&lt;title&gt;Cookie Check&lt;/title&gt;&lt;/head&gt;
&lt;body bgcolor="#FFFFFF" text="#000000"&gt;
Cookie Check Status:
&lt;?php
printf ('&lt;font color="#%s"&gt;%s&lt;/font&gt;&lt;br&gt;;',
$status ? "00FF00" : "FF0000",
$status ? "PASSED!" : "FAILED!");
?&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>



corry@worldweb.net
02-Jun-1999 10:14
setcookie does not work effectively for older browsers, such as aol 3.0 and IE 3.x. set the cookie manually in the header for consistent results.


thomas@vcsites.com
14-Jun-1999 09:57
From <A HREF=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2F%26quot%3Bhttp%3A%2Fdeveloper.netscape.com%3A80%2Fdocs%2Fmanuals%2Fjs%2Fclient%2Fjsguide%2Fcookies.htm%26quot&y=1999 TARGET="_new"> http://developer.netscape.com:80/docs/manuals/js/client/jsguide/cookies.htm</A> the document says that the domain name must have at least two periods before the cookie can be set and the cookie has to be set with two periods. I have to redirect everyone to the www.domain.com before setting the cookie.


justin@visunet.ie
23-Jun-1999 06:28
<b>Regarding re-directing and setting cookies.</b>


I have found that if you include everything in <b>one</b> header call then it works... Prehaps you could all have a look at this method and see if it works for you.
<pre>
Header("Location: http://www.domain.com/page.html\nSet-Cookie:COOKIE_ISBN[$i]=$isbn;time()+3600;path=/\n\n");
</pre>



dante@mpath.com
28-Jun-1999 04:14
Hey everyone, I dumped quite a few headers from some popular web sites, and I discovered that the cookies that PHP is sending out are not QUITE the same as these big sites. And, because of these differences, some browsers are not picking up the PHP cookies. I noticed that a lot of you have resorted to using the 'header' method of doing cookies, so, here is my contribution:



This code will act as a drop-in replacement for the PHP setcookie function, and you may call it in the same fashion. I just don't handle secure cookie settings, and I don't do array stuff. Other than that, I've tested this on IE3, IE4, IE5, NS3, and NS4. Works for me, and also works with the Location: redirect. Maybe we can incorporate this into the next PHP release?



Oh, and the notable changes are in the Date spec for expiration, and the ordering of domain, path, and expires. the old PHP way seemed to be out of order ;)



<pre>
function <font color="#5B69A6">MySetCookie</font> ($CkyName, $CkyVal, $exp, $pth, $Domain, $Secure) {
static $mycky;
$exp = strftime("%A, %d-%b-%Y %H:%M:%S", $exp);
$cookiestr = sprintf ("%s=%s; domain=%s; path=%s; expires=%s",
$CkyName, $CkyValue, $Domain, $pth, $exp);
$mycky = ( ($mycky) ? "$mycky\n" : "") . "Set-Cookie: $cookiestr";
header($mycky);
}
</pre>



stevem@mycomputer.com
29-Jun-1999 07:06
I've found that for some strange reason, passing parameters in the Location header (such as "Location: http://mydomain.com/test.html?dummy") will make the cookie work, at least with IE5 and Netscape 4.


nsayer@quack.kfu.com
03-Jul-1999 12:54
I have found that insuring that the server name in
the URL must match the server canonical name
in order for stuff to work most reliably. I use something
like this in a strategic location before I start fiddling
with cookies:

<pre>
if ($HTTP_HOST != $SERVER_NAME) {
header("HTTP/1.0 301");
header("Location: ".METHOD."://".$SERVER_NAME.$PHP_SELF);
exit;
}
</pre>



jeichorn@sps-inet.sturgis.k12.mi.us
10-Jul-1999 09:39
Just as a note, You might want to use the client browers time to set your expires, using your local time might mean your cookie wont expire for 24 hours.


tef@wesson.com
15-Jul-1999 03:30
Here's my workaround.....
<pre>Function redir($to) {
echo "<script language=\"javascript\"> <!--\n";
echo "window.location = \"$to\";";
echo "\n//--></script>";
}</pre>



tef@wesson.com
15-Jul-1999 03:34
Hmmm..something screwy about what I'm pasting.

A nice workaround is to echo a javascript that sets window.location to wherever you want the person to redirect to.

If you need..I could email you the code.



liffiton@simons-rock.edu
17-Jul-1999 02:49
As justin@visunet.ie stated, it seems to work much better to place multiple headers in a single Header() call. I was making multiple Set-Cookie statements in headers, and then using a Location: redirect, and only the last of the Set-Cookie statements was evaluated. I then placed all of the Set-Cookie headers in a single Header() call (using "/n" to separate them) and it worked fine (with the Location: header in its own Header(), mind you)...


rch@richard.eu.org
18-Jul-1999 09:11
I would sugest using separate cookie with value
set to expiration timestamp if you need short
time cookies. Your cookie may expire before it
is set because of wrong time/timezone on client.



nathans@usa.net
03-Aug-1999 05:42

<b>Clarification:</b> Bug is in IIS, not IE.


There are several references to a bug when header(&quot;Location:&quot;) is used in conjunction with setcookie. Several comments talk about this happening in IE4.


It's not an IE4 bug, it's an IIS bug. The bug also appears to exist in IIS 5 (Windows 2000). The bug is documented in KB article <a href=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2F%26quot%3Bhttp%3A%2Fsupport.microsoft.com%2Fsupport%2Fkb%2Farticles%2Fq176%2F1%2F13.asp%26quot%3B%26gt%3BQ176113%26lt%3B%2Fa%26gt%3B.%3C%2Fp&y=1999>

I found that using &lt;META http-equiv=&quot;Refresh&quot; content=&quot;0;URL:</i>your URL</i>&quot;&gt; seems to work as an alternative.




ryan@mwerx.com
13-Aug-1999 11:44
To set a double variable cookie try:

setcookie("UserPass", $user . ":" . $pass, time() + 3600, "", "", 0);

/* split up user/password */
$userpass = explode(":", $UserPass);

$userpass[0] is now the username
$userpass[1] is now the password





bram@guts.nl
23-Aug-1999 01:14
Just a nice tip for you folks. You can also
set cookies in a script that outputs image data
in stead of html.

<pre>
&lt;?
SetCookie("Name","value");
Header("Content-type: image/gif");
?>
</pre>

An image sets a cookie :)

Bram





jwebster@mirada-innovations.com
30-Aug-1999 03:13
In a previous note, someone mentions using a require() call
to cause the redirect to get around an IE4 problem. In their
sample code they have "Location: $redir_to\n\n" as the string.
This won't work because of the two trailing newlines.

Simply remove the newlines, and it will work.




jb@as220.org
01-Sep-1999 09:55
My previous comment is obsolete. Given
the 20-cookie limit, one really doesn't
want to use the <cookie>[subscript]
usage, since each element becomes a
separate cookie. Instead use serialize
and unserialize, and use
unserialize(stripslashes($cookie)) instead of plain unserialize()[see my
note there].



thomas@vcsites.com
28-Sep-1999 07:55
If you are having trouble setting cookies because people are coming to your site with http://domain.com instead of http://www.domain.com Try this code:

<PRE>
// what they're requesting is without the www.
if (getenv("HTTP_HOST") == "domain.com") {
//then Redirect them to the properly formatted url with any
//document/directory strings on the end.
Header("Location: http://www.domain.com" . getenv("REQUEST_URI"));

}
</PRE>

This can be put into an include file and included at the top of every page of the site in case they surf into a sub-page.



jimmcq@concentric.net
01-Oct-1999 05:52
If you don't set an expiration time, the cookie is only good until the session expries. It doesn't get saved, and it disappears when the user closes their browser.


jrochkin@cs.oberlin.edu
19-Oct-1999 11:15
DELETING COOKIES

This might be obvious to someone that understands cookies, but that's not me, or probably other users.

If you set a cookie with the domain paramter, in order to unset it, you need to call setcookie with an empty value paramater, but with the same domain paramter. You _can't_ simply call setcookie with one paramter, the cookie name, and delete the cookie (which is what the documentation says). It won't be deleted if you supplied a domain paramater on setting the original cookie.

In truth, I'm not sure if this actually deletes the cookie, or just sets the value of the cookie to the empty string, but for most php code this will amount to the same thing.



dan_wilson@geocities.com
28-Oct-1999 03:40
It seems that it is good practice to always pass all the variables, even if they are just empty strings or 0. I've run into problems where if I tried to leave off the optional variables, it didn't work. In addition, I found that I had to use time() for the expire rather than just 0.


deuschl@fsmb.mw.tu-muenchen.de
12-Nov-1999 04:01
You have to set &lt;?php_track_vars?> to be able to see your cookies in $HTTP_COOKIE_VARS.
You knew that, of course. I did't. Took me 'bout three hours.



dent@swcp.com
15-Nov-1999 10:58
If you are getting "Oops, setcookie called after header sent" messages and you think they are in error, check for whitespace before the function. I had some hiding at the bottom of an include file and it threw me off.



sheng.yuan@acemail.com
23-Nov-1999 09:15
IE4,When i use this URL(http://../setcookie.php3?url=a/get.php3), it doesn't work, if i use rawurlencode to get ride of "/" in the url, and then use rawurldecode to get it back in the setcookie.php3, it works.
hope it helps.



m.gianola@ilex.it
22-Dec-1999 04:52
Subj.: setcookie expire time.
"The expire argument is a regular Unix time integer as returned by the time()".
So expire argument is an absolute time.
If I want to leave a cookie alive for three hours how can I do?
If I set the expire argument as (time()+3*3600), this time is set according the clock of the my server and not according the clock of the client's computer. What will happen to the cookies set on the computers in China?
Is there any way to know the clock time of the client computer?
I hope someone will understand my problem.
Thanks and regards.
Marco



jclarke@cisco.com
27-Dec-1999 06:39
Marco, the time value that time() returns is the number of seconds since Jan 1, 1970 GMT. This value will always be the same no matter what the local timezone is. When evaluating this back to localtime, then the timezone will take effect. In other words, your code will work just as well for someone in China as it would for someone in Florida.


ccharlton@mach-nine.com
06-Jan-2000 06:01
Just thought I'ld share a recent problem I encountered with IE 5.01 and the SetCookie function in PHP3.

I began writing the script while testing with IE 5.0 and the following line would set the cookie correctly:

SetCookie ("AqmartShoppingCart","$session",time()+7200);

However when I upgraded to 5.01 it would no longer set the cookie. It appears as though IE 5.01 requires all fields the be filling in expect for the last one (secure) as in the following line of code:

SetCookie("AqmartShoppingCart","$session",$expiretime,"/","www.aqmart.com");

So even though the documentation states certain fields can be left blank it would appear as though it's best to use them all (aside from secure) to be assured of proper use.



robin@nerdcore.org
12-Jan-2000 11:25
To delete a cookie, just call setcookie (name-of-cookie-to-delete) or set the expiration time to 0.


denboer@educ8r.net
13-Jan-2000 03:56
If your set cookie is this :
<PRE>
SetCookie("cookiename", $cookieVal, 0, "/");
</PRE>
To unset the cookie use this :
<PRE>
SetCookie("cookiename", "", "", "/");
</PRE>



jens@omedia.dk
17-Jan-2000 07:32
well, infinty for a cookie, could be done by time()+somelargeint

somlargeint is in seconds the at the limit is just about 1166832000, thats is somtime about 2037 where most time functions will fail since they use a unsiged int. and have been counting since jan 1 1970 UTC. or try
#man time



lucas@slf.cx
30-Jan-2000 07:01
Re: 20 cookies works, 21 cookies fails.

According to Netscape's cookie spec, the limits for cookies are as follows:
<PRE>
300 total cookies (which is browser settable now I think).

4 kilobytes per cookie.

20 cookies per server or domain. Completely specified hosts and domains are considered separate entities, and each has a 20 cookie limitation.
</PRE>

You might want to have a close look at the cookies you are setting and see if any of them can be combined into one cookie.

Reference URL:
http://developer.netscape.com/docs/manuals/js/client/jsguide/cookies.htm



seb@junglemail.net
04-Feb-2000 11:05
To set a cookie don't use time()+123456789, it works better using the full date like here:
Header("Location: page.phtml\nSet-Cookie:name=value;expires=Friday, 16-Jan-2037 00:00:00 GMT;\n\n");



rudy@ccom.net
17-Feb-2000 08:53
I just realized that if you set a cookie, and you want to unset the cookie you will have to follow the order on the setcookie();
for example:
<pre>
setcookie("MyCookie","MyCookieValue","time()+3600","/tmp","www.domain.com");

then for unsetting the cookie:
setcookie("MyCookie","","","/tmp","www.domain.com");

</pre>
On the previous note about unsetting cookie, if you have double quotes on the time parameter, you have to the quotes on the unset cookie.
I have a question: wondering about setting/unsetting cookie using the Header Format. My workstation is running Windows 2000 and IE5.00.29, it seems that when it handles Header Cookies, it will take like 30 seconds to set the cookie. Anyone knows is this a bug on IE ? But when you try it on Windows 98/95 or NT, it will set the cookie fast!. Hope will get a respond.
Thanks
- Rudy



dennis@wacky.nu
29-Feb-2000 12:01
When using setcookie() IE 5 sometimes give some problems try this instead:

<PRE>$time = mktime()+600;
$date = date("l, d-M-y H:i:s", ($time));
header("Set-Cookie: cookiename=$value; expires=$date GMT");</PRE>

In this example the cooky whill expire in ten minutes, good luck with it.

PS: You better use this because it works always.

Dennis van der Vliet



admin@digitsix.com
01-Mar-2000 10:55
This is kinda stupid, but, I run a network off my DSL line with 1 ip, which means that all my windows boxes and what not run on NON-Routable IP addresses (eg. 192.168.1.xxx) anyway, when I am at my windows box, I can go to my domain by simply typing http://www/ and it will work. The problem I was having was that the cookies were not being saved. I realized that it was because of the fact that the cookie says ".digitsix.com" for the domain and my browser was looking at www - so you need to actually type in the full domain for it to work correctly with cookies. Anyway, Just thought I would share that

one other note:
the expire argument can take a string, for instance, "-1" for never expire... I origionally tried doing just -1 without quotes and the cookies were staying only temporary, so you need the quotes.

-digitsix



jrout@interserv.com.au
09-Mar-2000 08:43
In response to users editing the magic cookie file, I actually md5() anything that is to be stored as a cookie, and then DB the md5 and its associated real value. The user can edit the md5, but they won't know to what purpose.


olivier@lx.student.wau.nl
29-Mar-2000 06:26
If you want to set a complete array in only one cookie, use these functions:
<PRE>
function store_array_in_cookie($cookiename, $array2store) {
$tmpstring = serialize($array2store);
setcookie($cookiename, $tmpstring, time()+3600*5, "/", ".fbt.eitn.wau.nl");
unset($tmpstring);
}

function get_array_from_cookie($cookiename) {
$tmpstring = "global \$$cookiename";
eval($tmpstring);
unset($tmpstring);

if (isset(${$cookiename})) {
$tmparray=unserialize(stripslashes(${$cookiename}));
} else {
$tmparray = array();
}
return $tmparray;
}
</PRE>



medisoft@alohabbs.org.mx
14-Apr-2000 10:21
I use cookies with setcookie and they work with IE4,4.5 (mac) and 5...., but you may use an alternative function to set cookie
<pre>
function mySetCookie($name,$data,$exp)
{
$c=sprintf("<META HTTP-EQUIV=\"Set-Cookie\" CONTENT=\"%s=%s",$name,$data);
if($exp>0) {
// the 'y' must be lowercase if the browser don't work with full year, but then maybe will be problems with Y2k
$d=date("l, d-M-Y H:i:s CST",$exp);
$c.=";$d";
}
$c.="\">\n";
echo $c;
}
</pre>



moreaulf@starcraftse.com
14-Apr-2000 03:39
Thanks to dennis@wacky.nu!
The code he gave above,
<pre>$time = mktime()+600;
$date = date("l, d-M-y H:i:s", ($time));
header("Set-Cookie: cookiename=$value; expires=$date GMT");</pre>
Can aswell be used with the regular SetCookie for libs that use it:
<pre>$time = mktime()+600;
$date = date("l, d-M-y H:i:s", ($time));
setcookie("TestCookie","Test Value","$date GMT","/","www.yourdomain.com","");</pre>
And works perfectly with all tested browsers including IE4, 5, 5.5.
/More



mroszyk@hotmail.com
25-Apr-2000 09:03
I think I have got the solution to the COOKIE problem... If you wanted to set a cookie, then call a header redirect, all you need to do is the following:


&lt;?
$cook_value = "Sugar";

if($task == "")
{
setcookie ("TestCookie",$cook_value,time()+604800);
$task = "redirect";
}

if($task == "redirect")
{
header("Location: test.php3?task=nothing");
exit();
}

echo "$TestCookie - Cookie";
?>

If you have any problems with this, please contact me.

thanx,
matt



mroszyk@hotmail.com
25-Apr-2000 03:29
better formated...\n\n\n

<pre>
&lt;?
$cook_value = "Sugar";

if($task == "")
{
setcookie ("TestCookie",$cook_value,time()+604800);
$task = "redirect";
}

if($task == "redirect")
{
header("Location: test.php3?task=nothing");
exit();
}



echo "$TestCookie - Cookie";

?>
</pre>



akira@enmity.com
25-Apr-2000 05:16
<pre>
I finally figured out how to un-set a cookie properly (in 4.0 RC1 at least)

A lot of people suggested setcookie("cookie", "") or setcookie("var") - but this ONLY works in IE. I had to setcookie("var", 0) to get it to work in NS and IE.
</pre>



evan@dig-e-tal.com
02-May-2000 08:46
Another thing, that was kind of mentioned but not totally - I spent hours banging my head against the wall trying to get these to work - <i>make sure that the time argument is large!!</i> By adding a REALLY large int to mktime() I got cookies to save, ven when explicitly writing the date didn't work. Might have something to do with server time. Weird.


mads@danquah.dk
18-May-2000 12:29
The stuff about EI 5.01 and cookies :

the date-field seems to be the problem I simply did

$time = mktime()+1800;

$date = date("l, d-M-y H:i:s", ($time));

and then used $date in my cookie (using all fields)



saram34@intizen.com
20-May-2000 05:29
We generally use like this.
setcookie("admin_ck", $login_id);

This is ok.... But...
If suppose you setcookie on /a/b/c.phtml then you use this cookie on /a/b and a/b's sub-directory only.

So, I recommend below form without special reason.
setcookie("admin_ck", $login_id, time()*3600, "/~saram/", "www.saram.com");



mg@tmbg.org
11-Jul-2000 12:40
Note that cookies are global variables so you have to declare them as such when inside a function.


vk@work-in-the.net
11-Jul-2000 06:48
h.ebelt@addcom.de:
someone else posted a note above who had the same problem and wrote the solution is in the cookie specs:
you have to use at least two periods in the domain name, like in "www.domain.com" instead of "domain.com"
(".domain.com" doesn't seem to work either).



mg@tmbg.org
11-Jul-2000 11:37
Also note that when deleting a cookie, if you specified a path and or domain, you must do so again to delete it, i.e. <pre>setcookie("cook","","","/");</pre>
will delete the cookie that was created with
<pre>setcookie("cook",$val,$expire,"/");</pre>
whereas simply entering
<pre>setcookie("cook");</pre> won't



sleepy@linc-up.com
15-Jul-2000 08:07
To clear this thing with not being able to create cookies with both IE and NS. When I test my code with IE 5.0 and Win2k pro with PHP4pl2 I use: SetCookie("Var", "value", time()+86400, "/", "", 0);
And when I upload the script to my host I use: SetCookie("Var", "value", time()+86400, "/", "CorrectDomain", 0);

That seems to work with the browsers I have tested.



 About Notes


Previous page
 header_sent
 Updated
Sat, 12 Aug 2000
Hyperwave 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.