How to make a PHP show user data from database?

2.3k Views Asked by At

I made a login system, how do I make it show the username after login, so it goes like on the main login screen -> database check -> success page. On this success page how do I make it show the username, that was registered? I have only added

<?php
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>

On the 3rd page which is the success page, I need it to show the username that has logged on and other user information from the table.

4

There are 4 best solutions below

7
R R On

you need to store the username in the session variable then after successful login display it. you can use isset() to serve that purpose to check whether the session variable is set or not.

<?php
    session_start();
    echo $_SESSION['username'];
?>
0
Shankar Narayana Damodaran On

Something like this will do

<?php
session_start();
if(isset($_SESSION['myusername'])){
    echo "Welcome ".$_SESSION['myusername'];
}
else
{
   header("location:main_login.php");
}
?>

And don't use session_is_registered() as it is Deprecated.

0
Chandan Sharma On

Once you successfully logged in then store the username in the session variable. On the page first check whether session is set then show the username else redirect the user to login page.

 <?php
session_start();
if(isset($_SESSION['myusername']) && $_SESSION['myusername'] != ''){
      echo $_SESSION['myusername'];
}else{
      header("location:main_login.php");
}
?>
0
shubhraj On
<?php
     session_start();
     //if using post method in login it is better to check if $_POST is set
     $username = $_POST['myusername'];
     if(isset($_POST['myusername'])){
      if(!empty($username)){
       if(isset($_SESSION['myusername'])){
        echo $_SESSION['myusername'];
       }
      }
     }
 ?>

You can provide security by checking if your login details are not empty.