Jump to content

Users dont stay logged in.


Fredundant14

Recommended Posts

So lets say the user goes to my gallery page. It says "welcome guest, Login or Register"

 

The links to the login page works fine. The registration page works fine, and the logging in works fine. The user is logged in. But then when they return to the gallery page it says...

 

"welcome guest, Login or Register"

 

They have automatically been logged out.

 

What am I missing?

 

 

<?php

session_start();

 

$username = $_SESSION['username'];

 

$password = $_SESSION['password'];

 

if(!$username && !$password){

 

echo "Welcome Guest! <br> <a href=main_login.php>Login</a> | <a href=register.php>Register</a>";

}else{

echo "Welcome ".$username.";

}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/265058-users-dont-stay-logged-in/
Share on other sites

The most common reason navigating or redirecting around on a site does not carry session variables is because the URL's change between having and not having the www. on them and by default the session id cookie only matches the exact variation of your domain name where the session was first started.

 

Do the URLs consistently use the same variation of your domain, all with or all without the www. in them?

Fredundant:

 

I would suggest you put your login check into a single function that you include and use in all your pages.  This will eliminate the possibility that a variable typo or some other logic error causes a variation.  It will also help you pinpoint what to look for and what not to look for, as PFMaBiSmAd suggested.  Last but not least, it's part of the DRY philosophy that all good programmers try and follow.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.