cookie problems

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
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

cookie problems

Post by hob_goblin »

Ok, im having trouble retrieving info from a cookie...

here is how the cookie was set:
setcookie("loggedin['username']", "$username", time()+36000);

and after its set, i tried to do:
echo "welcome <b>".$loggedin['username']."</b><br />";
and it returns "welcome "
I also tried:
echo "welcome <b>".$HTTP_COOKIE_VARS['loggedin']['username']."</b><br />";
same problem...(i can't use $_COOKIE, my host hasn't upgraded)

what am i doing wrong?
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

setcookie("loggedin['username']", "$username", time()+36000);
maybe include a $ sign $loggedin['username']

and try without any "

setcookie($loggedin['username'],$username,time()+36000);

I don't use quotes and it works okay, I also name my cookies something like 'user'

setcookie('user',$username,time()+36000);

Then I check for

if ($user){

}
else{

}

I am struggling to see how $loggedin['username'] is going to work. lets say I log in and it creates a cookie called Mike, so when you do your check for a cookie it would need to check for $Mike, but how does your code know that for me it has to find a variable called $Mike, and don't say "because there is a cookie" because the servers doesn't know there is a cookie unless it knows what it is called and it doesn't know what it is called until it finds a cookie aaarrrgggh Catch 22.

Just call your cookie something like 'user' and check for the existence of that variable because it is the value associated with $user that is important.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

well i had loggedin['username'] and loggedin['password'],
well I'm going to go try out some of the things you said and see if they work
Post Reply