Jump to content

[SOLVED] Cookies aren't set


Andy17

Recommended Posts

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! :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

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.