Setting 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
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Setting Cookies !?!

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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);
}
?>
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.)
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

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