Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP session not registered in a login page

I made a simple login and registration system, and I used sessions to display the username to the user, just for practice.
It all works fine in the registration page, and I can register users, but when I try to login, it displays a blank page, Instead of what it is supposed to do, which is displaying 'Login success' and redirecting me to 'index.php', setting my $_SESSION and in the 'index.php' I use the session to display information about the user. The problem starts with that I'm not logged in after I insert the correct information in the form. This code worked great on my previous hosting service, but it had his problems.
This gets weirder when my friend can login to my account but I can't login.
So this is where is the 'Login success' part:

    else if ($login === true)
        {
            include 'include/aside.php';
            include 'include/footer.php';
            $id = id_from_username($username);
            $_SESSION['id'] = $id;
            redirect('index.php','2'); // a replacement for header()
            echo 'Login Success.';
            exit();  
        }

Here is the check if the user is logged in:

if(isset($_SESSION['id']))
{
    return true;
}
else 
{
    return false;
}

This is the login query:

function login($username, $password) {
$username = mysql_real_escape_string($username);
$password = md5($password);
$loginquery = mysql_query("SELECT id FROM `users` WHERE `username` = '$username' AND `password` = '$password' LIMIT 1");
return (mysql_num_rows($loginquery) == 1);}  

And yes, I use session_start(); at the start of each page. I tried this with Mozilla firefox, Google chrome and IE6, none of them worked. If you need more information, just tell me.

So why can't I login?

Thanks a lot.

(Oh, and excuse me for my english)

like image 815
shohamh Avatar asked Feb 27 '26 21:02

shohamh


1 Answers

Try replacing the include() statements with echo "success";. This will let you know if the failure is in one of those files, or possibly with the file path / permissions.

like image 63
dotancohen Avatar answered Mar 02 '26 10:03

dotancohen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!