3

I don't know where went wrong. When I tick the remember me checkbox The page didn't go to userarea page and seems like cookie didn't work as well It works when I just login without tick the remember me checkbox

<?PHP
session_start();
$connect=mysql_connect("localhost", "root", "");
mysql_select_db("phplogin", $connect);
function loggedin(){
 if(isset($_SESSION['email']) || isset($_COOKIE['username'])){
 $loggedin=TRUE;
 return $loggedin; 
 }
}
if(loggedin()){
header("Location:userarea.php");}
if(isset($_POST['login'])){
$email=$_POST['email'];
$password=$_POST['password'];
$rememberme=$_POST['rememberme'];
 if($email && $password){
 $login=mysql_query("SELECT * FROM users WHERE email='$email'");
 while($row=mysql_fetch_assoc($login)){
 $db_email=$row['email'];
 $db_password=$row['password'];
 $db_firstname=$row['firstname'];
 $db_lastname=$row['lastname']; 
 if($password==$db_password){$log=TRUE;}
 else{$log=FALSE;} 
 }
 if($log==TRUE){
 if($rememberme=="on"){
 setcookie("email", $email, time()+7200);
 }
 else if($rememberme==""){
 $_SESSION['email']=$email;
 header("Location:userarea.php");
 }
 }
 else{die("Wrong email or password");}
 }
}
?>
<html>
 <form action="index.php" method="POST">
 <input type="text" name="email" value="" placeholder="email" /><br/>
 <input type="password" name="password" value="" placeholder="password"/><br/>
 <input type="checkbox" name="rememberme"> Remember me | <a href="register.php">Register?</a><br/>
 <input type="submit" name="login" value="Login" />
 </form>
</html>
asked Dec 6, 2012 at 9:12
1
  • I also got an error when user didn't fill anything into text area Undefined index: $rememberme anyway to solve this problem? Commented Dec 6, 2012 at 9:55

2 Answers 2

4

Wrong cookie variable name in the function loggedin(). Its should be isset($_COOKIE['email'])

function loggedin(){
 if(isset($_SESSION['email']) || isset($_COOKIE['email'])){
 $loggedin=TRUE;
 return $loggedin; 
 }
}
answered Dec 6, 2012 at 9:33
Sign up to request clarification or add additional context in comments.

3 Comments

Please vote it as a useful answer so that I can get some points. :)
I also got an error when user didn't fill anything into text area Undefined index: $rememberme anyway to solve this problem?
Please check my second answer which will help you to fix the error :)
0

Change like the following

if(isset($_POST['rememberme'])){
 $rememberme = $_POST['rememberme'];
}

Then

if($log==TRUE){
 if(isset($rememberme)){
 setcookie("email", $email, time()+7200);
 }
 else{
 $_SESSION['email']=$email; 
 }
 header("Location:userarea.php");
 exit();
}
answered Dec 6, 2012 at 10:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.