Question

My login system only logs in if I do it 2 times. In the first time, it doesn't log in (automatically). I have a simple form with username and password, with action="login.php", which as you might think has the whole code for verify login, etc..and then, in the end, if everything is ok:

Cookie::set('page-main-login-cookie', serialize($arr), time()+60);
header('location: ../index.php');

In the index.php (which contains the form that calls login.php), in the top of the page:

ob_start(); session_start(); include('cookies.php'); include('sessions.php'); 

After the </html> tag:

if(Cookie::Exists('page-main-login-cookie')){
    $data = unserialize(stripslashes($_COOKIE['page-main-login-cookie']));
    if($data['status'] == 1){   
        Cookie::set('email', $data['email'], time() + (86400 * 7));
        Session::set('email', $data['email']);
        echo "<script type='text/javascript'> page_redirect(); </script>";
    }else{
        echo "<script type='text/javascript'> page_error(); </script>";
    }
    Cookie::Delete('page-main-login-cookie', time() - 3600);
}

And while I'm writing this topic I wonder if the problem might be in the javascript. The code for page_redirect() is the following:

function page_redirect(){
    alert("Done!");
    setTimeout(function(){
        location.reload();
    }, 1500);
}

What have I tried so far?

  1. Changed header('location: ../index.php'); to header('location: http://www.mysite.com');
  2. Added session_write_close(); after Session::set('email', $data['email']);

None of them worked.

Was it helpful?

Solution

Probably because your log in code comes after your html, so it appears like you are not logged in even though the script does log you in at the end of the script.

Typically you would want to keep your PHP and HTML as separate as possible (in different files), and do all of your PHP business logic first, then output all the HTML.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top