简体   繁体   中英

display full name after login using sessions

I have this login system using php, mysqli and sessions and it works fine but would be good if I can get the users full name displayed on the welcome page after login

I amended my code to the following on the login.php page and the welcome page code is after the login code but is not showing the full name

<?php

// Initialize the session
session_start();

// Check if the user is already logged in, if yes then redirect him to welcome page
if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
    header("location: user-account.php?user=$username");
    exit;
}

// Include config file
require_once "registerconfig.php";

// Define variables and initialize with empty values
$username = $password = "";
$username_err = $password_err = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){

    // Check if username is empty
    if(empty(trim($_POST["user_name"]))){
        $username_err = "Please enter username.";
    } else{
        $username = trim($_POST["user_name"]);
    }

    // Check if password is empty
    if(empty(trim($_POST["user_pass"]))){
        $password_err = "Please enter your password.";
    } else{
        $password = trim($_POST["user_pass"]);
    }

    // Validate credentials
    if(empty($username_err) && empty($password_err)){
        // Prepare a select statement
        $sql = "SELECT user_id, user_name, user_pass, customer_name 
                FROM users WHERE user_name = ?";

        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;
            $param_customername = $customername;

            // 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, $customername);
                    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["user_id"] = $id;
                            $_SESSION["user_name"] = $username;
                            $_SESSION["customer_name"] = $customername;

                            // Redirect user to welcome page
                            header("location: user-account.php?user_name=$username");                            
                        } else{
                            // Display an error message if password is not valid
                            $password_err = "The password you entered was not valid.";
                        } 
                    }
                } else{
                    // Display an error message if username doesn't exist
                    $username_err = "No account found with that username.";
                }
            } else{
                echo "Oops! Something went wrong. Please try again later.";
            }

            // Close statement
            mysqli_stmt_close($stmt);
        }

    // Close connection
    mysqli_close($link);
}
?>

Welcome page code below

<?php echo htmlspecialchars($_SESSION["customer_name"]); ?>

Variable $customername is not defined and no value assigned to it. Because of that on line $_SESSION["customer_name"] = $customername; empty value assigned to $_SESSION["customer_name"] .

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