Andy17 Posted June 7, 2009 Share Posted June 7, 2009 Hey guys, I have played with cookies before but I am still relatively new to it. My problem is that my cookies are not being set, even though PHP gives me no error. Here is the code I have. <?php // If the remember checkbox is marked when logging in, then set the cookies if (!empty($_POST['remember'])) { $time = 60 * 60 * 24 * 365 * 20 + time(); setcookie("h_n1", "email", $time, "/", ".mydomain.com", false, true); setcookie("h_n2", "password", $time, "/", ".mydomain.com", false, true); } ?> Any ideas? And yes, I have used sha1 on the password. Just a bonus question, though: should I use sha1 on the email as well? I know it's normally not necessary, but I thought it might be when storing it in a cookie (I realize that the e-mail cannot be used for much, but anyways...). Thank for in advance, freaks! Quote Link to comment https://forums.phpfreaks.com/topic/161277-solved-cookies-arent-set/ Share on other sites More sharing options...
jmr3460 Posted June 7, 2009 Share Posted June 7, 2009 Does LAST_INSERT_ID() work if the insert was on a different session? Quote Link to comment https://forums.phpfreaks.com/topic/161277-solved-cookies-arent-set/#findComment-851015 Share on other sites More sharing options...
Andy17 Posted June 7, 2009 Author Share Posted June 7, 2009 Umm, I can't edit my post, but the if statement IS in fact true and it does run the code within the brackets. I tested it by adding an echo after the setcookie functions. I'm not sure what you mean by your post to be honest... I chose to insert text and not variables with text just to break it down as much as I could. Quote Link to comment https://forums.phpfreaks.com/topic/161277-solved-cookies-arent-set/#findComment-851047 Share on other sites More sharing options...
Ken2k7 Posted June 7, 2009 Share Posted June 7, 2009 Instead of empty, try isset. Also, don't hash emails because you'll never get the original emails back in case you need to send emails to them. Quote Link to comment https://forums.phpfreaks.com/topic/161277-solved-cookies-arent-set/#findComment-851049 Share on other sites More sharing options...
gevans Posted June 7, 2009 Share Posted June 7, 2009 20 years is a bit drastic, but apart from that are you still having a problem? I'd recommend storing a unique identifier rather than the password, and maybe the email just to validate it, why not! Quote Link to comment https://forums.phpfreaks.com/topic/161277-solved-cookies-arent-set/#findComment-851050 Share on other sites More sharing options...
Andy17 Posted June 7, 2009 Author Share Posted June 7, 2009 Instead of empty, try isset. I tried isset the first time but when it didn't work, I just used empty because that's how I did it in my previous project (even though it should work with isset). This isn't the problem anyways, because as I stated, PHP does find the if statement to be true and therefore run the setcookie code. The cookies are just not set for some unknown and very mysterious reason! 20 years is a bit drastic, but apart from that are you still having a problem? Yeah, I know. I'm still quite lost on this one. I just don't see why it doesn't work. Does the placement of the setcookie functions matter as long as they are in the head? Quote Link to comment https://forums.phpfreaks.com/topic/161277-solved-cookies-arent-set/#findComment-851185 Share on other sites More sharing options...
gevans Posted June 7, 2009 Share Posted June 7, 2009 Well, the easy way to cehck if that's working, is to check your cookies locally. clear them, run the page, and check if it's loaded. It's very easy to view your cookies if you're using firefox. If it is working fine the problem is with the script attempting to recognize and read the cookie. Quote Link to comment https://forums.phpfreaks.com/topic/161277-solved-cookies-arent-set/#findComment-851203 Share on other sites More sharing options...
Andy17 Posted June 8, 2009 Author Share Posted June 8, 2009 Well, the easy way to cehck if that's working, is to check your cookies locally. clear them, run the page, and check if it's loaded. It's very easy to view your cookies if you're using firefox. If it is working fine the problem is with the script attempting to recognize and read the cookie. I didn't code the script checking for them yet, I just checked for cookies using Firefox as you said. I also tried with IE just to make sure it's not just Firefox acting up. Quote Link to comment https://forums.phpfreaks.com/topic/161277-solved-cookies-arent-set/#findComment-851269 Share on other sites More sharing options...
gevans Posted June 8, 2009 Share Posted June 8, 2009 Let's just try a little test; <?php $value = "Test Cookie Value" setcookie("TestCookie", $value, time()+3600); ?> Save that as test.php and run it. If that doesn't save you know that it's not a problem with your script! Quote Link to comment https://forums.phpfreaks.com/topic/161277-solved-cookies-arent-set/#findComment-851271 Share on other sites More sharing options...
Andy17 Posted June 8, 2009 Author Share Posted June 8, 2009 Your simple script worked and made me figure out what was wrong! Instead of .mycurrentdomain.com, I had put .mynewdomain.com, which I haven't even bought yet. I should just have put in my old domain since that is the one I am using for the testing of my project. Thank you for your help and I apologize that I did not spot this. I really should have. Quote Link to comment https://forums.phpfreaks.com/topic/161277-solved-cookies-arent-set/#findComment-851418 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.