Page 1 of 1
[SOLVED] setcookie not setting the cookie
Posted: Sun Sep 11, 2005 4:23 am
by anjanesh
Code: Select all
setcookie("user", $_POST['user'], 3600, "", "", 0);
setcookie("pass", $_POST['pass'], 3600, "", "", 0);
if (!(isset($_COOKIE['user'])))
die("Not Set");
Anyone know why is the script dying showing
Not Set ?
Thanks
[EDIT] Added :
a.php
Code: Select all
setcookie("testa", "a", 3600, "", "", 0);
header("b.php");exit;
b.php
Code: Select all
setcookie("testb", "b", 3600, "", "", 0);
Does testa get set in the cookie ?
Posted: Sun Sep 11, 2005 7:00 am
by raghavan20
the key is you forgot to put the expire time as "time() + 3600"
Code: Select all
<?php
if (isset($_POST['user']))
$user_value = $_POST['user'];
else
$user_value = username;
if (isset($_POST['pass']))
$pass_value = $_POST['pass'];
else
$pass_value = password;
setcookie("user", $user_value, time() + 3600, "", "", 0);
setcookie("pass", $pass_value, time() + 3600, "", "", 0);
echo $_COOKIE["user"];
echo $_COOKIE["pass"];
if (!(isset($_COOKIE['user'])))
die("Not Set");
?>
Posted: Sun Sep 11, 2005 7:11 am
by anjanesh
Ah...Thanks very much.
Strange - I thought 3600 would be 3600 secs from now.
Posted: Sun Sep 11, 2005 12:45 pm
by josh
That code is pointless, you can't know whether or not a client accepted a cookie until a subsequent http request is made, that is how the http protocol works
Posted: Sun Sep 11, 2005 12:57 pm
by anjanesh
Yes - I figured that only after doing this :
Code: Select all
<? print_r($_COOKIE); ?>
<script type="text/javascript">
document.write(document.cookie);
</script>
Didnt think !