[SOLVED] setcookie not setting the cookie

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

[SOLVED] setcookie not setting the cookie

Post 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 ?
Last edited by anjanesh on Sun Sep 11, 2005 7:11 am, edited 1 time in total.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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"); 
?>
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Ah...Thanks very much.
Strange - I thought 3600 would be 3600 secs from now.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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 !
Post Reply