Re: another easy cookie question
| From: | Richard Baskett | Date: | Fri, 14 Sep 2001 18:04:55 +0000 |
| Subject: | Re: another easy cookie question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-67398@lists.php.net to get a copy of this message | ||
The way I see it is that your script never has a chance to set the cookie
plus you can not send headers after html, one other thing is that you have a
")" at the end of your setcookie function value so if you reformatted it
this way:
<?
setcookie("Cookievalue", "abcdefghijklmnopqrstuvwxyz0123456789",
time()+3600);
header("Location: index.php");
exit;
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
What that will do is as soon as it comes to the page it will set a cookie
and then take you to index.php. You do not need to use the full URL in the
Location. Plus I have had nothing but trouble with the setcookie function
so I just send the header setcookie instead, for example I use this code:
header("Set-Cookie: $CookieName=$CookieValue; path=/; expires=$expireDate");
Make sure expire is in this format and it'll work on all browsers:
$expireDate = gmdate("l, d-M-");
$expireDate .= gmdate("Y")+1;
$expireDate .= gmdate(" H:i:s")." GMT";
this sets the expiration date for one year.
All in all your new code would look like:
<?
$expireDate = gmdate("l, d-M-");
$expireDate .= gmdate("Y")+1;
$expireDate .= gmdate(" H:i:s")." GMT";
header("Set-Cookie: $CookieName=$CookieValue; path=/; expires=$expireDate");
header("Location: index.php");
exit;
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
> <html>
> <head>
> <?php
> header("Location:index.php");
> setcookie("Cookievalue", "abcdefghijklmnopqrstuvwxyz0123456789")
> time()+3600);
> ?>
> <title>Untitled Document</title>
> <meta http-equiv="Content-Type" content="text/html;
> charset=iso-8859-1">
> </head>
>
> I get the following error.
>
> CGI Error
> The specified CGI application misbehaved by not returning a complete set
of
> HTTP headers. The headers it did return are:
>
> ... and nothing else!
>
> Where am I going wrong? I bet it's a simple answer.
>
> George P in Edinburgh