Click here to Skip to main content
15,889,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I'm trying to show the users username instead of the user email but I can't seem to get that working why can I only use $_SESSION['email'];?

code login
PHP
<pre><?php
session_start();
// Check if the user is already logged in, if yes then redirect him to welcome page
if(isset($_POST["email"]) && $_POST["password"] === true){
    header("location: welcome.php");
    exit;
}

 // Include config file
include "config.php";

if ($_SERVER['REQUEST_METHOD'] === 'POST') { 
    $email = mysqli_real_escape_string($conn , $_POST['email']);
    $password = mysqli_real_escape_string($conn , $_POST['password']);

    // link voor de prepared statemts met select https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php
    $sql  = 'SELECT * FROM users WHERE email = ?';
    $stmt = $conn->prepare($sql); 
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $result = $stmt->get_result(); // get the mysqli result
    if ($row = $result->fetch_assoc()) {
        if (password_verify($password, $row['password'])) {
                        // login
                        $_SESSION['loggedin'] = true;
                        $_SESSION['ID'] = $row['ID'];
                        $_SESSION['username'] = $row['username'];
                        $_SESSION['name'] = $row['name'];
                        $_SESSION['email'] = $email;
                        header("Location: welcome.php");
            }else {
                $message = "Invalid password or username";
                echo '<script language="javascript">';
                echo "alert('$message');\n";
                echo 'window.location.href="login.php"'; //Redirects the user with JavaScript
                echo '</script>';
            }
            
        $conn->close();
    }else{
        $message = "Invalid password or username";
        echo '<script language="javascript">';
        echo "alert('$message');\n";
        echo 'window.location.href="login.php"'; //Redirects the user with JavaScript
        echo '</script>';
    }
}
 ?>


this is where I want to greet people
<?php 
session_start();
if(!isset($_SESSION['id'])){

//if (isset($_SESSION['loggedin']) && $_SESSION['username'] == $username) {
    echo "Welcome to the member's area, " . $_SESSION['email'] . "!";
}


Like I said $_SESSION['email']; shows up just fine but $_SESSION['username']; doesn't show up why is this happening?

What I have tried:

Tried looking a few times on the internet
used stack overflow
but because english is not my first language its is pretty hard to type into google what I need
Posted
Comments
Richard Deeming 2-Dec-21 8:48am    
That's going to depend on the data in your table, which we can't see.

Debug your code and check what value the $row['username'] returns.

Also, avoid using select * from ... in your queries; specify the precise list of columns you need to load instead.

BTW, congratulations on avoiding the two biggest problems we often see with this sort of script (SQL injection and plain-text passwords). :) However, since you're correctly using parameters, you don't want to call mysqli_real_escape_string anywhere.
thatraja 9-Dec-21 2:24am    
/* but $_SESSION['username']; doesn't show up why is this happening? */
You getting any error message? If yes, share it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900