Jump to content

setcookie and isset($_COOKIE(name)) seem very finnicky.


rizlmilk

Recommended Posts

I'm currently playing around with a user system with login and registration. I'm trying to use cookies to log the user in but the cookie either doesn't set or it sets after I travel through a few pages. Also, when I use isset($_COOKIE(name)) it sometimes doesn't return that it is set even though it is.

 

login.php (the username and password request page which I am currently using to display if a cookie is set)

<?php
if (isset($_COOKIE["grocerycookie"]))
{
$username = $_COOKIE["grocerycookie"];
echo "You are logged in, <b>$username</b><br />";
}
else
{
echo "You have not been logged in.";
}
?>

 

 

login2.php (sets the cookie)

<?php
mysql_connect("*", "*", "*") or die(mysql_error());
mysql_select_db("*") or die(mysql_error());
$username = strtolower($_POST['username']);
$password = $_POST['password'];
$data = mysql_query("SELECT * FROM * WHERE username='$username'") or die(mysql_error());

while($info = mysql_fetch_array($data))
{
if ($password == $info['password'])
{
	setcookie("grocerycookie", $username, time()+3600);
}
}

 

Am I setting the cookie wrong or checking for it wrong?

Link to comment
Share on other sites

The most likely cause I can think of is the domain that your currently viewing/using. If you have certain pages on sub-domains then those pages probably won't by default have access to the same cookie as the main domain. You can also run into problems if your site uses frame redirecting, Internet Eplorer will not by default accept cookies from a domain being ran in a frame, but the other major browsers do.

Link to comment
Share on other sites

The 4th and 5th parameters of setcookie are for the path and domain (actually a subdomain/hostname) that the cookie matches (the browser only sends cookies that have settings that that match the URL being requested.) Have you read the php.net documentation to see if either setting has something to do with your symptoms?

Link to comment
Share on other sites

After a little more testing I have found out that it works totally fine in IE6, but FireFox 3.5 just sets the cookie whenever it feels like it. I am printing either "loggedin = 1" if the username and password matched and therefore the cookie was set, otherwise it will print "loggedin = 2" to indicate the cookie wasn't set. In my testing it will always show "loggedin = 1" when I have correctly entered a valid username and password combo, but it seems to randomly decide whether the cookie will be set. As I said earlier, IE6 always sets the cookie correctly but FireFox doesn't always set the cookie.

 

PFMaBiSmAd: I have read the documentation but, like usual, a lot of it typically goes over my head. The best way to learn something for me is to play with it...which leads to questions...which leads me here. Playing around with the remaining parameters seemed to get me further away.

 

<?php

$username = $_POST["username"];
//setcookie("grocerycookie", $username, time()+3600);

mysql_connect("_", "_", "_") or die(mysql_error());
mysql_select_db("_") or die(mysql_error());
$username = strtolower($_POST['username']);
$password = $_POST['password'];
$data = mysql_query("SELECT * FROM userpass WHERE username='$username'") or die(mysql_error());
$loggedin = 0;

while($info = mysql_fetch_array($data))
{
if ($password == $info['password'])
{
	setcookie("grocerycookie", $username, time()+3600);
	$loggedin = 1;
}
}

if ($loggedin == 1)
{
echo "loggedin = 1"; //the cookie should be set
}
else
{
echo "loggedin = 2";
}
?>

 

Try it out for yourself: http://blowthecart.com/grocery/login.php

Username: sho

Password: sho

or register your own user/pass.

 

Hopefully this information will help. Thanks for any suggestions.

Link to comment
Share on other sites

How are you determining you have a cookie issue, because the code you have posted below doesn't actually read the cookie. It simply checks against the database sets a cookie then continues based on the value set in PHP no the cookie. Your site as-is works fine in all 4 of my browsers as far as I can tell from what you have set up.

Link to comment
Share on other sites

I know I'm not actually reading the cookie in that provided code, I was just proving that the setcookie area of code was reached. The remaining pages use isset to check the cookie.

 

I'm using Ubuntu 9.10. Both FireFox and Chrome don't log in correctly.

Link to comment
Share on other sites

I know I'm not actually reading the cookie in that provided code, I was just proving that the setcookie area of code was reached. The remaining pages use isset to check the cookie.

 

I'm using Ubuntu 9.10. Both FireFox and Chrome don't log in correctly.

 

Why would you not try to set the domain and reference on documentation for setcookie to make sure you're not performing a mistake?

 

But if you're doing it with a cart, as mentioned, if you want a more reliable method than just use sessions, PHP will handle the cookies for you, and sessions can expire.

Link to comment
Share on other sites

A session is no more or less reliable than a cookie because by default a session id is propagated between page requests using a cookie. So, whatever systematic problem the OP is having setting a cookie will also prevent his use of a session. If he was attempting to use a session, this thread would instead be titled "my sessions don't persist between page requests".

 

rizlmilk, your page did not send a cookie when I tired it. You likely have a header error occurring on the page. For debugging purposes, add the following two lines of code immediately after the first opening <?php tag on the page that sets the cookie -

 

ini_set("display_errors", "1");
error_reporting(E_ALL);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.