Roxenne Palacios
Roxenne Palacios

Reputation: 21

How to include logged user name in accounts page

I'm trying to make a user Login page wherein when you log-in to the website you will be redirected to a user account page. In this user account page, i need to print the name of currently logged in user. But how do I do that? Here's my php code:

<?php
require_once 'init.php';
$user = new User();
if(!$user->isLoggedin()){
Redirect::to('login.php');
}

if($_SESSION["loggedin"] == false ){ //redirects to index if login is incorrect
        redirect_to("index.html");
    }
    //if successful, assign values to variable
    $display_name = $_SESSION["loggeduser"];
    $loguid = $_SESSION["userID"];

?>

here's the code of the body page:

 <div id="about" class="container-fluid ">
  <div class="row">
    <div class="col-sm-8">

      <h2>Welcome <?php echo $display_name ?> to your account! </h2><br>
      <br>
      <div>
      <h2><a href="signup.php">REGISTER STUDENT ACCOUNT </a></h2>
      </div>
      <div>
      <h2><a href="medicaldictionary.php">INSERT MEDICAL TERM </a></h2>
      </div>
      <div>
      <h2><a href="logout.php">LOG-OUT </a></h2>
      </div>

when I tried to run it, it says undefined index loggedin and undefined function redirect_to

I can't quite understand why. Can anybody help me?

Upvotes: 1

Views: 41

Answers (2)

Rahul
Rahul

Reputation: 726

Check login id/email which is used in Log in page and using that fetch corresponding user name from database table. In $display_name variable fetch data from db table.

For example -

 $sql = "SELECT student_name FROM student_table where stduent_id = ".$userID;
 $display_name = mysqli_query($conn,$sql);

You can also do this using session Assign UserID to SessionID and using that sessionID you can fetch other data of that logged in user.

Upvotes: 1

Dimag Kharab
Dimag Kharab

Reputation: 4519

First of all include session_start(); at top of your code and secondly check your Session for loggedin like this ,

if (!isset($_SESSION['loggedin']) || !$_SESSION['loggedin'] ) {

     Redirect::to('index.html'); 

     /* not redirect_to("index.html")
      I guess you already have a Class Redirect that has a static method to()*/
}

Upvotes: 0

Related Questions