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.
need help with cookies
Moderator: General Moderators
Re: need help with cookies
Checkbox items only appear in $_POST when they ARE checked.
Code: Select all
$rememberme = (isset($_POST['rememberme'])) ? $_POST['rememberme'] : FALSE;-
beginner123
- Forum Commoner
- Posts: 70
- Joined: Fri Feb 24, 2012 9:56 am
Re: need help with cookies
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:
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 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'];
}- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: need help with cookies
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'] : '';
}(#10850)
Re: need help with cookies
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: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: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 websiteCode: Select all
$rememberme = (isset($_POST['rememberme'])) ? $_POST['rememberme'] : FALSE; if($rememberme == "on") { setcookie("userName","userName", time() +7200); } else if($rememberme == "") { $_SESSION['userName']; }
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'];
}
Last edited by califdon on Sat Dec 01, 2012 11:48 am, edited 2 times in total.
Reason: Added a paragraph at the bottom
Reason: Added a paragraph at the bottom