Abderrahmane Bourri
Abderrahmane Bourri

Reputation: 128

How can i display the User's name after loggin in using PHP?

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?

Upvotes: 1

Views: 60

Answers (2)

Waids
Waids

Reputation: 118

  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>";

  }

?>```

Upvotes: 1

Arun Vishwakarama
Arun Vishwakarama

Reputation: 166

Update this line with

$username = $_SESSION['username'];

With this

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

Upvotes: 0

Related Questions