简体   繁体   中英

Can't pass and display username from login page to homepage

Im trying to pass a login username through the login page to a dashboard in order to display the first name and last name through a sql query. My username variable is empty and I cannot figure out why. Any help would be wonderful.

Login-submit.php page!

 if ($count){ $_SESSION['username'] = $username;// $_SESSION['loggedin'] = true or false would work too $_SESSION['password'] = $password; // Why store the password in session data? $user= $_SESSION['username'] header("Location: pageMLC_Dashboard.php"); exit(); }else{ $message="InvalidLogin"; header("Location: pageMLC.php?message"); exit(); } 

Dashboard Page

<?php
session_start(); // Right at the top of your script
include('config.php');
if(!isset($_SESSION['username'])){ //if login in session is not set
    header("Location: pageMLC.php");
//$username=$_SESSION['username'];
}
?>

//Specify the query

$query = "SELECT first_name, last_name FROM person p, login l, shift_info si, shift_date sd, id_table id 
WHERE l.username = '$username' AND p.person_id=l.person_id;"; //AND si.shift_id=id.shift_id AND p.person_id=id.person_id AND sd.date_id=id.date_id";

//Store the result

$result = mysql_query($query) or die("SQL Error: <b>" . mysql_error() . "</b><br />");
?>
<!-- LANDING ----------------------------------------------
<!---------------------------------------------------------
<!--------------------------------------------------------->
<nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <a href="pageMLC_Dashboard.php"><img src="Images/logo.png" class="images" ></a>
          <a href="pageMLC_Dashboard.php"><button type="button" class="btn">Home</button></a></li>
          <a href="https://mobile.fairview.org" target="_blank"><button type="button" class="btn">Intranet</button></a>
          <a href="#"><button type="button" class="btn">Settings</button></a>
          <a href="#"><button type="button" class="btn">Profile</button></a>
          <a href="logout.php"><button type="button" class="btn">Logout</button></a>
        </div>
    </nav>

    <div class="container-fluid" id="myGroup">
      <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
          <ul class="nav nav-sidebar">
              <li class="active"><button type="button" class="btn btn-block" data-toggle="collapse" data-target="#dashboard">Dashboard</button><span class="sr-only">(current)</span></li>
              <li class="button"><button type="button" class="btn btn-block" data-toggle="collapse" data-target="#calendar">Calendar</button></li>
              <li class="button"><button type="button" class="btn btn-block" data-toggle="collapse" data-target="#updates">Organizational Updates</button></li>
              <li class="button"><button type="button" class="btn btn-block" data-toggle="collapse" data-target="#shift">Open Shift Page</button></li>
              <li class="button"><button type="button" class="btn btn-block" data-toggle="collapse" data-target="#super">Supervisor Editing Page</button></li>
          </ul>

        </div>  
          <div id="dashboard" class="collapse indent">
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <h1 class="page-header">Overview</h1>
        <p>                    <?php
echo " <h1>Hello ".$_SESSION["username"].",","</h1>";?>Welcome to the overview page! Here we will set up some backend code to remove all the pages and just have toggle-able panels or div classes that you interact with using the buttons in the nav/ sidebars.</p>

<table border="4" cellpadding="3" cellspacing="1">

<tr><th>First Name</th><th></th><th>Last Name</th>

<?php while ($row = mysql_fetch_array($result)) { ?>

  <tr><td><?php echo $row["first_name"] ?></td>

  <td><?php echo $row["last_name"] ?></td>

  </tr>

<?php } ?>

Remember to run the session_start() function at the top of each page (maybe include it in a php file you call at the start of each page?). This function creates or resumes the current session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM