Pulling information from cookies.

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
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Pulling information from cookies.

Post by Jim »

Hey all.

So here's the dealio -

I have two cookies that are set when one logs in to my page:

IamJim_user and IamJim_pass

This is the code I *tried* using to pull the information:

Code: Select all

<?
if($_COOKIE&#1111;'IamJim_user']) &#123;
	if($_COOKIE&#1111;'IamJim_pass']) &#123;
	$user_name = $_COOKIE&#1111;'IamJim_user'];
	$password = $_COOKIE&#1111;'IamJim_pass'];
	&#125;
&#125;
?>
I thought this would check for the first cookie, then find it and check for the second cookie, find it and then set those variables equal to the values of the cookies.

Later on in the script, I have

Code: Select all

<?
echo "Welcome '$user_name'!";
?>
And I get:

Welcome "! (for an example, visit http://www.empiregaming.net/iamjim and log in at the bottom of the script as aaa, pass: aaa.)

Any ideas?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

check wether there has been any cookie-data sent with

Code: Select all

echo '&lt;pre&gt;'; print_r($_COOKIE); echo '&lt;/pre&gt;';
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

Output comes from phpbb :P

Here's what I used to set the cookies. Maybe this code is incorrect?

Code: Select all

&lt;?php
	setcookie("IamJim_user" , $user_name , +3600);
	setcookie("IamJim_pass" , $password , +3600);
?&gt;
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

this will mark the cookie as invalid one hour after January 1 1970 00:00:00 GMT ;)

take a look at the examples at http://www.php.net/manual/en/function.setcookie.php
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

here is a quick way to do what you want, but you should still read what volka gave you.

Code: Select all

<?php
   setcookie("IamJim_user" , $user_name , time() + 3600);
   setcookie("IamJim_pass" , $password , time() + 3600);
?>
this adds one hour to the current time as the expiration date.
Post Reply