Problem with cookies and header() (newbie)

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
mrclash
Forum Newbie
Posts: 4
Joined: Sat Oct 09, 2004 9:48 am
Location: Barcelona, Spain

Problem with cookies and header() (newbie)

Post by mrclash »

Hi,
I'm having a problem with cookies. I use setcookie() in signmein.php to set 2 cookies and just after that I call header():

Code: Select all

setcookie ("iguser", md5($_POSTї'username']), $time+3200);
setcookie ("igpass", md5($_POSTї'password']), $time+3200);
			
header ("Location: http://foo.com/admin.php");
The problem is that in admin.php I'm trying to get those cookie values and they seem to be empty (not set).

Code: Select all

session_start();

if (isset($_COOKIEї'iguser']) &&  isset($_COOKIEї'igpass']))
{
	if (!($_COOKIEї'iguser'] == md5($user_name) && $_COOKIEї'igpass'] == md5($user_pwinput)))
	{
		echo $_COOKIEї"iguser"];
		echo $_COOKIEї'igpass'];
		//header("Location: signin.php");
		//exit();
	}
}


It has to be a very obvious thing, I know (maybe an include?) but any help will be greatly received. As I said in the topic, I'm a newbie in PHP :P

Thanx.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the cookie setting may not happen before your browser redirects. Are you sure they are setting in the first place?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Which webserver and version are you using? Some have issues with this.

Mac
mrclash
Forum Newbie
Posts: 4
Joined: Sat Oct 09, 2004 9:48 am
Location: Barcelona, Spain

Problem with cookies and header() (newbie)

Post by mrclash »

Ok, I didn't try it before, the second page has nothing to do with the problem...In fact, I'm not setting the cookies, but I don't know why!

Code: Select all

setcookie ("iguser", md5($_POST['username']), $time+3200);
setcookie ("igpass", md5($_POST['password']), $time+3200);

if (isset($_COOKIE["iguser"]) &&  isset($_COOKIE["igpass"])) 
	{echo "Cookies registered";}
else	{echo "Argh";}
The setcookie() function doesn't do anything. Any hint?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Post a link to this cookie setting script and I will tell you the headers it's actually sending out. I'd bet your server removes the cookie headers or your browser ignores them in some fashion because of the redirect you are doing.
mrclash
Forum Newbie
Posts: 4
Joined: Sat Oct 09, 2004 9:48 am
Location: Barcelona, Spain

Problem with setcookie()

Post by mrclash »

You can try it at http://www.mans-hq.net/igrafic/admin/signin.php.

Code: Select all

login: dev
password: dev00
I don't think that it is a problem with the browser, is Firefox 1.0PR with all cookie settings activated. I have revised it thousand times...

Thanx ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't have the time right now to fiddle with my header script to get posting to a remote page to work, so I'll post my observation: The browser handles the redirect before your cookies are set.
mrclash
Forum Newbie
Posts: 4
Joined: Sat Oct 09, 2004 9:48 am
Location: Barcelona, Spain

Post by mrclash »

I see...and is there a general solution if the browser handles the redirect before the cookies are set??

Thanks :)
Post Reply