Page 1 of 1

PHP & Cookies

Posted: Fri Mar 10, 2006 1:55 am
by srg_13
Hi,

I have a problem with setting cookies with PHP. When I call the setcookie command, no cookie is set. Why is this?

This is the code I am using to set the cookie:

Code: Select all

setcookie("cookie_name", $cookie_data);
.
But when I try to retrive the cookie, it does not exist.

I have looked this up on numerous sites, and all of them say that this is right. Do you have any ideas why it would not be working?

I am using PHP5, Apache 2, and MySQL on Windows.

-Stephen

Re:PHP & Cookies

Posted: Fri Mar 10, 2006 2:54 am
by AGI1122
Try this:

Code: Select all

setcookie('cookie_name',$cookie_data,time()+30240000);
If that doesn't work, then there could be some other reason it's not working. Make sure to set the cookie before you display any content to the browser, otherwise the cookie won't be set.

Re:PHP & Cookies

Posted: Fri Mar 10, 2006 4:51 am
by crackers
Steve the Pirate wrote:
This is the code I am using to set the cookie:

Code: Select all

setcookie("cookie_name", $cookie_data);
.
But when I try to retrive the cookie, it does not exist.
Maybe you're not reloading your site after setting cookie.

Re:PHP & Cookies

Posted: Fri Mar 10, 2006 6:44 am
by srg_13
Its still not working...

Can you take a look at this code?

Code: Select all

<?

include "../include/mysql.php";

$user=$_POST['user'];
$pass=$_POST['pass'];
$rem=$_POST['remember'];

$result = db_get("homepage", "users");
$num = db_numrows($result);

$password = mysql_result($result, $num-1, "password");


if(crypt($pass, $password)==$password && $user=="Stephen")
{
   $auth="true";
   if($rem=="on")
   {
      setcookie("stephen", $password, time()+60*60*24*30*12);

   }
   else
   {
      setcookie("stephen", $password);
   }
   $o=1;
}



if($auth=="true")
{
   echo "Logged In - Redirecting <meta http-equiv=\"Refresh\" content=\"0; url=/\">";   
   
}
else
{
   echo "ACCESS DENIED <meta http-equiv=\"Refresh\" content=\"0; url=/login/?s=1\">";
}


?>
I can't figure out why it is not working... I tried

Code: Select all

      if(setcookie("stephen", $password))
      {
         echo "Set";
      }
      else
      {
         echo "Error";
      }
and it says set, but no cookie is set.

I am sure no cookie is set, as I have a web development toolbar that can list all the cookies set by a particular domain.

-Stephen

Re:PHP & Cookies

Posted: Fri Mar 10, 2006 6:54 am
by Solar
Steve the Pirate wrote: I tried

Code: Select all

??????if(setcookie("stephen", $password))
??????{
?????????echo "Set";
??????}
??????else
??????{
?????????echo "Error";
??????}
and it says set, but no cookie is set.

I am sure no cookie is set, as I have a web development toolbar that can list all the cookies set by a particular domain.
*cough*

So you believe your web development toolbar (which says there is no cookie), but not your webserver (which says there is one)?

Re:PHP & Cookies

Posted: Fri Mar 10, 2006 6:30 pm
by Kon-Tiki
Session stuff should go before the headers're sent. It's the very first line of your code that it should be put in. Also, make sure your browser has cookies enabled.

Re:PHP & Cookies

Posted: Fri Mar 10, 2006 8:55 pm
by srg_13
So you believe your web development toolbar (which says there is no cookie), but not your webserver (which says there is one)?
Well, there is nothing in the variable $_COOKIE["stephen"] or $HTTP_COOKIE_VARS["stephen"] either.

Cookies are turned on in my browser.

-Stephen

Re:PHP & Cookies

Posted: Fri Mar 10, 2006 10:19 pm
by srg_13
Do you think I should try this:

Code: Select all

$time = time()+30240000;
header("Set-Cookie: stephen=$password; expires=$time; path=/;");
-Stephen

Re:PHP & Cookies

Posted: Sat Mar 11, 2006 10:55 pm
by srg_13
Yes! that header worked! The time()+3600 thing wouldn't work, so I did this instead:

Code: Select all

$year = date("Y");
$time = date("d-M-Y", mktime(0, 0, 0, 1, 1, $year+2));
header("Set-Cookie: stephen=$password; expires=$time; path=/;");
Basically, that makes it expire in two years, if you couldn't figure that out...

Does anyone have any idea why the setcookie command wouldn't work, but this way did? Isn't this what setcookie sende??

-Stephen

Re:PHP & Cookies

Posted: Sun Mar 12, 2006 8:04 am
by Kon-Tiki
I've said it before, and I'll say it again. Session variables and cookies need to be set first thing in your code, before any of the headers're passed on. By putting it in a header-function, it should work. I'm just wondering where you placed it. If you placed it somewhere mid-way your code, it'd be very odd and shouldn't be working :P

Re:PHP & Cookies

Posted: Mon Mar 13, 2006 1:06 am
by srg_13
Here's the working finished code. The $_POST['remember'] was sent from a checkbox marked "Remember me". The rest is pretty self-explainitory:

Code: Select all

<?
include "../include/mysql.php";

$user = $_POST['user'];
$pass = $_POST['pass'];
$rem = $_POST['remember'];

$result = db_get("homepage", "users");
$num = db_numrows($result);

$password = mysql_result($result, $num-1, "password");


if(crypt($pass, $password)==$password && $user=="Stephen")
{
   $auth="true";
   
   if($rem=="on")
   {
      $year = date("Y");
      $time = date("d-M-Y", mktime(0, 0, 0, 1, 1, $year+2));
      header("Set-Cookie: stephen=$password; expires=$time; path=/;");
      echo "$time";
   }
   else
   {
      header("Set-Cookie: stephen=$password; path=/;");
   }
   $o=1;
}



if($auth=="true")
{
   echo "Logged In - Redirecting <meta http-equiv=\"Refresh\" content=\"0; url=/\">";   
   
}
else
{
   echo "ACCESS DENIED <meta http-equiv=\"Refresh\" content=\"0; url=/login/?s=1\">";
}


?>
Basically, it gets the user information from the MySQL database. (Kind of pointless, considering there is only one user and people cannot create accounts... But that may change eventually...). Then it checks it against the user submitted details, and sets a cookie if it is correct. It then refreshes the page.

-Stephen