Page 2 of 2

Re: need help with cookies

Posted: Fri Nov 30, 2012 5:59 pm
by califdon
That error is telling you that the script didn't receive any value for the checkbox 'rememberme' (the name of the input element is the 'index' to the $_POST array, so if there is no element there, there is no such index).

One way to see this is to temporarily insert this brief line right at the beginning of your script (inside the php tags, of course):

print_r($_POST);

That will print on the screen the values present in the array variable $_POST, so you can see if you have all the indexes and values you are expecting. I think you will find that when you check the checkbox, the print_r function will show that you have 3 indexes and values in $_POST, but when you don't check the box, there will only be 2 indexes and values--no 'rememberme' index!

Now, there are a couple of other issues in your script that you need to take care of. I assume you excerpted this code from a complete script? Because I don't see any opening/closing <?php ?> tags, I don't see any MySql database connection. Assuming that you have taken care of this, I see you have properly escaped the username before using it in a query, but you neglected to do the same for the password, which is just as important.

Re: need help with cookies

Posted: Sat Dec 01, 2012 2:43 am
by twinedev
Checkbox items only appear in $_POST when they ARE checked.

Code: Select all

$rememberme = (isset($_POST['rememberme'])) ? $_POST['rememberme'] : FALSE;

Re: need help with cookies

Posted: Sat Dec 01, 2012 6:41 am
by beginner123
yes i did not post the whole code but the php tags and the database connection code are done. Everything on the page works execpt for the rememberme part.

so i changed the code to this:

Code: Select all

$rememberme = (isset($_POST['rememberme'])) ? $_POST['rememberme'] : FALSE;
						
if($rememberme == "on")
{
	setcookie("userName","userName", time() +7200);
}
else if($rememberme == "")
{
	$_SESSION['userName'];
}
but the cookie is not being created. if i sign in and click the remember me check box and then exit the browser, i am logged off when i return to the website

Re: need help with cookies

Posted: Sat Dec 01, 2012 11:26 am
by Christopher
You are comparing FALSE with "" and also checking specifically for "on" which may be problems. Simpler might just be:

Code: Select all

if(isset($_POST['rememberme'])) {
	setcookie("userName", $userName, time() +7200);
} else {
	$userName = isset($_SESSION['userName']) ? $_SESSION['userName'] : '';
}

Re: need help with cookies

Posted: Sat Dec 01, 2012 11:40 am
by califdon
beginner123 wrote:yes i did not post the whole code but the php tags and the database connection code are done. Everything on the page works execpt for the rememberme part.

so i changed the code to this:

Code: Select all

$rememberme = (isset($_POST['rememberme'])) ? $_POST['rememberme'] : FALSE;
						
if($rememberme == "on")
{
	setcookie("userName","userName", time() +7200);
}
else if($rememberme == "")
{
	$_SESSION['userName'];
}
but the cookie is not being created. if i sign in and click the remember me check box and then exit the browser, i am logged off when i return to the website
So the variable $rememberme is defined as either the value passed in $_POST['rememberme'], if that exists, OR the boolean value FALSE. It will never be equal to "". You will only know which of these 2 values it is when and if you submit the form and run the script again. I assume that you did submit the form, but you say that it then does not find the cookie? You must think very precisely in these matters--you said that "the cookie is not being created", but I think you really don't know that, you only know that your script, with the logical "if" structure you have written, does not recognize that you are logged in. Those are 2 entirely different statements. To determine what's wrong, you have to do some debugging: temporarily insert small "echo" statements that will show you exactly what parts of the script are executing and what values are assigned to key variables. I would do something like this:

Code: Select all

$rememberme = (isset($_POST['rememberme'])) ? $_POST['rememberme'] : FALSE;
      echo "<br />$rememberme<br />";    //   Debug - remove later                                         
if($rememberme == "on")
{
     echo "<br />The rememberme value was on and the cookie will now be set...<br />";   //  Debug - remove later
        setcookie("userName","userName", time() +7200);
}

// I don't know what you were trying to do with this next part:

else if($rememberme == "")
{
        $_SESSION['userName'];
}
When you run this and check the box, now you will be able to see if the value "on" appears (meaning that it is recognizing the checkbox) and whether it then follows the logic and presumably sets the cookie. If it does get that far, then either setcookie() isn't working (but it looks to me like it should--although it will always have the same value: "userName", did you perhaps intend to put the value of the user name in the cookie??--OR the cookie IS being set, but in another part of the script where it is searching for the cookie, it didn't find it, which would indicate that you have a problem in THAT part of your script.