Programming, for all ages and all languages.
srg_13
Post
by srg_13 » Fri Mar 10, 2006 1:55 am
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
AGI1122
Post
by AGI1122 » Fri Mar 10, 2006 2:54 am
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.
crackers
Post
by crackers » Fri Mar 10, 2006 4:51 am
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.
srg_13
Post
by srg_13 » Fri Mar 10, 2006 6:44 am
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
Solar
Member
Posts: 7615 Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:
Post
by Solar » Fri Mar 10, 2006 6:54 am
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)?
Every good solution is obvious once you've found it.
Kon-Tiki
Post
by Kon-Tiki » Fri Mar 10, 2006 6:30 pm
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.
srg_13
Post
by srg_13 » Fri Mar 10, 2006 8:55 pm
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
srg_13
Post
by srg_13 » Fri Mar 10, 2006 10:19 pm
Do you think I should try this:
Code: Select all
$time = time()+30240000;
header("Set-Cookie: stephen=$password; expires=$time; path=/;");
-Stephen
srg_13
Post
by srg_13 » Sat Mar 11, 2006 10:55 pm
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
Kon-Tiki
Post
by Kon-Tiki » Sun Mar 12, 2006 8:04 am
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
srg_13
Post
by srg_13 » Mon Mar 13, 2006 1:06 am
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