rizlmilk Posted January 6, 2010 Share Posted January 6, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/187370-setcookie-and-isset_cookiename-seem-very-finnicky/ Share on other sites More sharing options...
cags Posted January 6, 2010 Share Posted January 6, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187370-setcookie-and-isset_cookiename-seem-very-finnicky/#findComment-989573 Share on other sites More sharing options...
PFMaBiSmAd Posted January 6, 2010 Share Posted January 6, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/187370-setcookie-and-isset_cookiename-seem-very-finnicky/#findComment-989696 Share on other sites More sharing options...
rizlmilk Posted January 7, 2010 Author Share Posted January 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187370-setcookie-and-isset_cookiename-seem-very-finnicky/#findComment-990129 Share on other sites More sharing options...
cags Posted January 7, 2010 Share Posted January 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187370-setcookie-and-isset_cookiename-seem-very-finnicky/#findComment-990200 Share on other sites More sharing options...
rizlmilk Posted January 7, 2010 Author Share Posted January 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187370-setcookie-and-isset_cookiename-seem-very-finnicky/#findComment-990445 Share on other sites More sharing options...
cags Posted January 7, 2010 Share Posted January 7, 2010 As we both said (or at least implied) earlier it is probably down to the domain the cookie is active on. Are all files that it is not working on within the grocery directory? Quote Link to comment https://forums.phpfreaks.com/topic/187370-setcookie-and-isset_cookiename-seem-very-finnicky/#findComment-990446 Share on other sites More sharing options...
rizlmilk Posted January 7, 2010 Author Share Posted January 7, 2010 Yes, every file I have is in the grocery directory. Quote Link to comment https://forums.phpfreaks.com/topic/187370-setcookie-and-isset_cookiename-seem-very-finnicky/#findComment-990665 Share on other sites More sharing options...
Lamez Posted January 7, 2010 Share Posted January 7, 2010 I use $_SESSION[]'s instead of $_COOKIE[]'s It works well. Quote Link to comment https://forums.phpfreaks.com/topic/187370-setcookie-and-isset_cookiename-seem-very-finnicky/#findComment-990683 Share on other sites More sharing options...
cags Posted January 8, 2010 Share Posted January 8, 2010 Hmm... well in that case I don't know. Without seeing the usage of the cookies I can't think of anything. Quote Link to comment https://forums.phpfreaks.com/topic/187370-setcookie-and-isset_cookiename-seem-very-finnicky/#findComment-990809 Share on other sites More sharing options...
oni-kun Posted January 8, 2010 Share Posted January 8, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187370-setcookie-and-isset_cookiename-seem-very-finnicky/#findComment-990828 Share on other sites More sharing options...
PFMaBiSmAd Posted January 8, 2010 Share Posted January 8, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/187370-setcookie-and-isset_cookiename-seem-very-finnicky/#findComment-990910 Share on other sites More sharing options...
rizlmilk Posted January 11, 2010 Author Share Posted January 11, 2010 Before I tried PFMaBiSmAd's suggestion I tested the site a little more and it now seems to work fine. I didn't change anything at all but it is now working perfectly. Thanks for all the help everyone. Don't be surprised if I'm back though Quote Link to comment https://forums.phpfreaks.com/topic/187370-setcookie-and-isset_cookiename-seem-very-finnicky/#findComment-992615 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.