Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.