PHP login system fetch username and redirect issue

Is there a way to get current logged in username and based on that redirect to a different page?

I’m using the following secure PHP login without MySql as a login system: https://sourceforge.net/projects/phploginbyvallastech/

Now I’m looking to redirect each logged in user to their personalized page.

But I can’t figure out how to A) fetch the current logged in user and B) redirect multiple users.

This code redirects to the latter address, but the username based redirect is not working:

<?php
session_start();

if ($_SESSION["username"]==User1){ 
    header("location: user1content.php");
    exit;

} else {
    header("location: generalcontent.php");
    exit;
} 
{ ?>

<?php } ?> 

So it’s clearly not fetching the logged in user. Though <?php echo $login->username; ?> fetches the username just fine.
I know there’s something missing, but what that might be… Have been trying different things without success.

Or another viable option would be to show content based on username:

<?php session_start(); if ($_SESSION["username"]=='User1'){ ?>

SHOW USER1 CONTENT HERE

<?php } ?>

But this has the same issue; not sure how to fetch the logged in username…

Maybe this will help

session_start();

/* User Name */
$user = "John Smith";  // Assume $user is assigned $login->username value:: 

/* Session Name */
$_SESSION["username"] = "John Smith";

if ($_SESSION["username"] === $user ){ 
    header("location: user1content.php");
    exit;

} else {
    header("location: generalcontent.php");
    exit;
}

Thank you so much for this. I feel like this might finally be close to a solution :smiley:
It now actually does redirect the user to user1content.php, but that seems to happen to all users. Just have to figure out how to add multiple user redirects :thinking:

So this is rather static overall. How do you know what their personalized page is? If you follow a normal routing policy, you would do something like this:

if ( $_SESSION['User']['hasPersonalPage'] ) {
    header( "location: user/{$_SESSION['User']['id']}/page");
    exit;
}

Huzzah! :star_struck:
I think the end solution was a very simple one, as it most of the cases seems to be:

<?php 

require('_login.php'); 

if ($login_user === 'User1') {
    header("Location: user1.php");
} else if ($login_user === 'User2') {
    header("Location: user2.php");
} else if ($login_user === 'User3') {
    header("Location: user3.php");
} else {
    header("Location: generalcontent.php");
}

?>

yay for progress!

how does user1.php / user2.php / user3.php differ from eachother? asking because if they are the same page but with the info for the spesific user then the normal way to do this is to fetch each users info and just use the same page (user.php) for all users as template

All the pages will have different content (downloadable files, table graphs etc.). I’m sure there might be a better way to do what I wanted to achieve, but this works for my needs so I’ll take it :sweat_smile:

How many users? How frequently are they added? If this stays incredibly small, naming them is fine, though has code smell. If you have even 5 users, there are better ways to do it whether that be a dictionary, or coming from a database(preferably).

I’m currently working on a dashboard that the main content window changes, but everything else stays constant.

I am hoping to have more users than 5 :sweat_smile: So might have to look into some database type login system at some point…
I was suggested to use mySQL and “password_hash”… But as my login pages hold no state secrets, I think the outdated system shall work for a while.

password_hash is just for the users passwords. You want to use it because it protects the user. Users in general reuse their passwords, so you storing it in plain text means that someone can get it and test it against known accounts. It has nothing to do with your site specifically, but the safety and privacy of your registered users.

Hopefully that makes you rethink it? If you need help on how to do the change in place, it isn’t that difficult to implement.

Sponsor our Newsletter | Privacy Policy | Terms of Service