Home > OS >  Session Full Name instead of Username in PHP Login Form
Session Full Name instead of Username in PHP Login Form

Time:05-02

I have a login form and I want to display the name of the user and not the username on the welcome page. Below is my HTML code

<body>
<h1 >Hi, <b><?php echo htmlspecialchars($_SESSION["name"]); ?></b>. Welcome to our site.</h1>
<p>
    <a href="reset-password.php" >Reset Your Password</a>
    <a href="logout.php" >Sign Out of Your Account</a>
</p>

PHP Code:

// Validate credentials
if(empty($username_err) && empty($password_err)){
    // Prepare a select statement
    $sql = "SELECT id, username, password FROM users WHERE username = ?";
    
    if($stmt = mysqli_prepare($link, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "s", $param_username);
        
        // Set parameters
        $param_username = $username;
        
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            // Store result
            mysqli_stmt_store_result($stmt);
            
            // Check if username exists, if yes then verify password
            if(mysqli_stmt_num_rows($stmt) == 1){                    
                // Bind result variables
                mysqli_stmt_bind_result($stmt, $id, $username, $hashed_password);
                if(mysqli_stmt_fetch($stmt)){
                    if(password_verify($password, $hashed_password)){
                        // Password is correct, so start a new session
                        session_start();
                        
                        // Store data in session variables
                        $_SESSION["loggedin"] = true;
                        $_SESSION["id"] = $id;
                        $_SESSION["username"] = $username;                            
                        
                        // Redirect user to welcome page
                        header("location: welcome.php");

What changes do I need to session the Name and not the Username?

CodePudding user response:

<?php
session_start();
?>
<body>
<h1 >Hi, <b><?php echo htmlspecialchars($_SESSION["name"]); ?></b>. 
Welcome to our site.</h1>
<p>
<a href="reset-password.php" >Reset Your Password</a>
<a href="logout.php" >Sign Out of Your Account</a>
</p>

first in your users table, alter a column with name "name" if name column is not 
exist and give input from your signup form.

// Validate credentials

if(empty($username_err) && empty($password_err)){

$sql = $link->query("SELECT id, name, username, password FROM users WHERE username = '$username'");
                    
        // Check if username exists, if yes then verify password
if ($sql->num_rows > 0){
    $row = $result->fetch_assoc();

    if(password_verify($password, $row['password'])){
        // Password is correct, so start a new session
        session_start();
        

enter code here
        // Store data in session variables
        $_SESSION["loggedin"] = true;
        $_SESSION["id"] = $row['id'];
        $_SESSION["username"] = $row['username']; 
        $_SESSION["name"] = $row['name']; 
        
        // Redirect user to welcome page
        header("location: welcome.php");
    
    }
 }
}

CodePudding user response:

use this code

index.php

<!DOCTYPE html>
<html>
<body>

<h2>HTML Forms</h2>

<form action="login.php" method="post">
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="username" value="John"><br>
  <label for="lname">Last name:</label><br>
  <input type="password" id="lname" name="password" value="Doe"><br><br>
  <input type="submit" value="Submit">
</form> 

<p>If you click the "Submit" button, the form-data will be sent to a page called "/action_page.php".</p>

</body>
</html>

login.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "mydb";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);

// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$username_err = $_POST['username'];
$password_err= $_POST['password'];


$sql = "SELECT * FROM users where username='$username_err'";
$result = $conn->query($sql);

  // output data of each row
  while($row = $result->fetch_assoc()) {
   session_start();
                        
                        // Store data in session variables
                        $_SESSION["loggedin"] = true;
                        $_SESSION["id"] = $row['userId'];
                        $_SESSION["username"] = $row['username']; 
                        $_SESSION["name"] = $row['name'];                       
                        
                        // Redirect user to welcome page
                        header("location: welcome.php");
  }
$conn->close();

?>

welcome.php

<?php
session_start();
?>
<h1 >Hi, <b><?php echo htmlspecialchars($_SESSION["name"]); ?></b>. Welcome to our site.</h1>
<p>
    <a href="reset-password.php" >Reset Your Password</a>
    <a href="logout.php" >Sign Out of Your Account</a>
</p>
</p>

note:- you have to replace database name 'mydb' to your database name.

  • Related