Page 1 of 1

Setting Cookies !?!

Posted: Mon Nov 10, 2003 6:55 pm
by nigma
Hey,

I have the following code:

Code: Select all

<?php
if (validate($user))
{
  setcookie("officer", $user,$cook_life,"/csoff/","",0);
  header("location: http://www.google.com");
}
else
{
  print "Authentication failed.";
}
?>
Now, when I access this page and do what is needed to get the validate function to return true it takes me to http://www.google.com, yet the cookie "officer" never gets set. I assumed the reason for this was that my browser was set not to allow cookies, so I then tried setting a cookie using PHP on a different server, just:

Code: Select all

<?php
setcookie("officer", $user,$cook_life,"/csoff/","",0);
?>
and everything works fine. So I know it is not the browser. I then went back to the other server and changed the setcookie() part of it to:

Code: Select all

<?php
if (!setcookie("officer", $user,$cook_life,"/csoff/","",0))
{
  print "Failed";
}
else
{
  print "Succesfull.";
}
?>
And it still said succesfull.

Are there any other things that may be preventing the cookie from being set that I am overlooking?

Thanks for any and all help provided.

Posted: Tue Nov 11, 2003 5:40 am
by JAM
Seems that there is alot of wierd cookie handling these days...

What does this show you?

Code: Select all

<?php
if (!setcookie("officer", $user,$cook_life,"/csoff/","",0)) {
    print "Failed";
} else {
    print "Succesfull.<pre>";
    print_r($_COOKIE);
}
?>

Posted: Thu Nov 13, 2003 8:47 am
by nigma
Jam, sorry for late response, got carried away with another project, but I tried what you suggested and I get an empty array. I also have checked my cookies folder after it says the cookies have been set and no cookie has been set.

Any other ideas?

Posted: Thu Nov 13, 2003 10:29 am
by JAM
Empty array, but succesful? Insert;

Code: Select all

echo '<pre>'; 
echo 'Display Errors: '.(ini_get('display_errors') == '1' ? 'On' : 'Off')."\n"; 
echo 'Error Level: '.(ini_get('error_reporting') == '2047' ? 'E_ALL' : 'Not E_ALL')."\n"; 
echo '</pre>';
, and see what that generates. I'm guessing that you get an error msg, but have the error level to low to see it.
(Still wierd that the setcookie() evals to true, even if it should fail.)

Posted: Thu Nov 13, 2003 9:28 pm
by nigma
Display Errors: On
Error Level: Not E_ALL

Now, the particular server that the script is running on is not mine or a friends, it is rented, so if what is causing this is a setting in the ini file then I will probably have to find an alternative to cookies (possibly sessions).

Thanks a bunch for the help, apreciate it.

Posted: Fri Nov 14, 2003 3:16 am
by twigletmac
To see more errors put:

Code: Select all

error_reporting(E_ALL);
at the top of your script (you don't have to edit the php.ini for this which is nice).

Mac

Posted: Fri Nov 14, 2003 1:24 pm
by Weirdan
JAM, you can get the $_COOKIE["something"] only when the client sent the cookie. It means at least after refresh button pressed. So your test case wasn't correct.