skip to Main Content

I want to show the name of the user after logging in (e.g. Hey User!) using PHP, i tried the $username = $_SESSION['username']; but it keeps giving me nothing (Hey !).

Here’s my PHP code:

<?php

session_start();

$connection = mysqli_connect('localhost','root','admin');

mysqli_select_db($connection, 'user_registration');

$email = $_POST['email'];
$password = $_POST['password'];

$verify_emailpass = " select * from user_table where email = '$email' && password = '$password'";
$result = mysqli_query($connection, $verify_emailpass);
$num = mysqli_num_rows($result);

if($num == 1){
  $_SESSION['loggedin'] = true;
  $username = $_SESSION['username'];
  echo "Hey $username !";

}else{
  echo "<b>Incorrect Email or Password.</b>";

  }

?>

Here’s the Html log in From:

<form action="validation.php" method="post">
  <div class="row content">
    <div class="col-12">
      <b><p class="text-align">Se connecter:</p></b>
      </div>
    <div class="col-12">
    <input name="email" type="text" class="input-box shadow-sm" placeholder="Adresse E-mail" required/>
  </div>
    <div class="col-12">
    <input name="password" type="password" class="input-box shadow-sm" placeholder="Mot de passe" required/>
  </div>
  </div>
  <div class="button-2">
    <button id="loginButton" type="submit" class="next-button shadow-sm"><img src="res/icons/button-arrow.svg" width="45px" height="45px" /></button>
  </div>
</form>

And I was wondering how can i get any information i want from the user after he logs in in order to place it in other pages like Profile page or so?

2

Answers


  1. Update this line with

    $username = $_SESSION[‘username’];

    With this

    $username = $result[‘username’]; $_SESSION[‘username’] =
    $username;

    Login or Signup to reply.
    1. If this page is login than session is empty

    I think it’s check mysql email and password
    Than this code

    $data[‘username’]

    username is mysql name column name

    <?php
    
    session_start();
    
    $connection = mysqli_connect('localhost','root','admin');
    
    mysqli_select_db($connection, 'user_registration');
    
    $email = $_POST['email'];
    $password = $_POST['password'];
    
    $verify_emailpass = " select * from user_table where email = '$email' && password = '$password'";
    $result = mysqli_query($connection, $verify_emailpass);
    $num = mysqli_num_rows($result);
    
    if($num == 1){
    $data =  $result->fetch_assoc()
    $_SESSION['username'] = $data['username'];
      $_SESSION['loggedin'] = true;
      $username = $_SESSION['username'];
      echo "Hey $username !";
    
    }else{
      echo "<b>Incorrect Email or Password.</b>";
    
      }
    
    ?>```
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search