-3

I have 2 sessions that store first name and last name from the MySQL database. I want to display the first name and last combined on the dashboard page when the user logs in. Please assist.

Below is the code that checks login.

    if(isset($_POST['login']))
{
  if(!empty($_POST['email']) && !empty($_POST['password']))
  {
    $email    = trim($_POST['email']);
    $password   = trim($_POST['password']);

    $md5Password = md5($password);

    $sql = "SELECT * from tbl_users where email = '".$email."' and password = '".$md5Password."'";
    $rs = mysqli_query($conn,$sql);
    $getNumRows = mysqli_num_rows($rs);

    if($getNumRows == 1)
    {
      $getUserRow = mysqli_fetch_assoc($rs);
      unset($getUserRow['password']);

      $_SESSION = $getUserRow;
       // $_SESSION['alogin']= $_POST['email'];
      $_SESSION['id']=$getUserRow['id'];
      $_SESSION['first_name']=$getUserRow['first_name'];
      $_SESSION['last_name']=$getUserRow['last_name'];

      header('location:dashboard.php');

      exit;
    }
    else
    {
      $errorMsg = "Wrong email or password";
    }
  }
}

Dashboard code:

<p>
              You are logged in as
              <?php echo $_SESSION['first_name']?>
              <!-- Alexander Pierce - Web Developer -->
              <small>Member since Nov. 2012</small>
            </p>
  • 1
    Your codes open to sql injections you need to use prepared statements and use php password_hash instead of md5. –  Apr 10 '20 at 11:14
  • **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman Apr 10 '20 at 23:14

2 Answers2

0

As you have saved session in both first_name and last_name key . You can also use that in your project. Please check the following code

<p>
              You are logged in as
              <?php echo $_SESSION['first_name'].$_SESSION['last_name'];?>
              <!-- Alexander Pierce - Web Developer -->
              <small>Member since Nov. 2012</small>
            </p>
Ariful Islam
  • 696
  • 6
  • 11
0

Dashboard code:

<p>
              You are logged in as
              <?php echo $_SESSION['first_name'] ." ".$_SESSION['last_name']?>
              <!-- Alexander Pierce - Web Developer -->
              <small>Member since Nov. 2012</small>
            </p>
Umar Farooque Khan
  • 515
  • 1
  • 6
  • 17
  • let me ask, Do I need to store each field of the table in a session? How do I go about storing everything in one session or as an array instead of each field for each session. – Sospeter Mong'are Apr 11 '20 at 07:33