最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Firstname Not Displaying on Homepage After Signup - Stack Overflow

programmeradmin3浏览0评论

The issue is that after a user registers, their email is successfully stored in $_SESSION and displayed on the homepage, but firstname does not appear.

After successful registration, both email and firstname should be stored in the session and displayed on home.php.

What Happens Instead: $_SESSION['email'] works correctly and displays on the homepage. $_SESSION['firstname'] is missing or defaults to 'Guest' in home.php.

Code: register.php (Handles user signup and session creation)

session_start();

if (!isset($_SESSION['user_id'])) {
    header("Location: login_style.html");
    exit();
}

// Retrieve user input
$firstname = trim($_POST['firstname']);
$email = trim($_POST['email']);
$password = trim($_POST['password']);
$repeat_password = trim($_POST['repeat-password']);

if ($password !== $repeat_password) {
    die("Passwords do not match!");
}

// Database connection
$conn = pg_connect("host=localhost port=5432 dbname=webappDB user=postgres password=your_password");

if (!$conn) {
    die("Connection failed: " . pg_last_error());
}

// Hash password
$hashed_password = password_hash($password, PASSWORD_DEFAULT);

// SQL Insert (Possible issue here?)
$query = "INSERT INTO users (firstname, email, password) VALUES ($1, $2, $3)";
$result = pg_query_params($conn, $query, array($firstname, $email, $hashed_password));

if ($result) {
    // Retrieve user data
    $query = "SELECT user_id, firstname FROM users WHERE email = $1";
    $result = pg_query_params($conn, $query, array($email));

    if ($row = pg_fetch_assoc($result)) {
        $_SESSION['user_id'] = $row['user_id'];
        $_SESSION['firstname'] = $row['firstname']; // Set firstname in session
        $_SESSION['email'] = $email;
        $_SESSION['logged_in'] = true;

        var_dump($_SESSION); // Debugging
        header("Location: home.php");
        exit();
    } else {
        echo "Error fetching user data!";
    }
} else {
    echo "Registration Failed: " . pg_last_error($conn);
}

pg_close($conn);
?>

home.php (Displays session data)

session_start();

if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
    header('Location: login_style.php');
    exit();
}

$user_email = $_SESSION['email'] ?? 'Guest';
$user_name = $_SESSION['firstname'] ?? 'Guest'; // This does not work
?>

What I've Tried:

  1. Checked var_dump($_SESSION); in register.php → Shows firstname is set correctly.
  2. Checked for session start issues → session_start(); is at the top of both register.php and home.php.
  3. Tried reloading home.php → No change, still defaults to 'Guest'.
  4. Checked database query syntax → The insert and select queries seem correct.

The issue is that after a user registers, their email is successfully stored in $_SESSION and displayed on the homepage, but firstname does not appear.

After successful registration, both email and firstname should be stored in the session and displayed on home.php.

What Happens Instead: $_SESSION['email'] works correctly and displays on the homepage. $_SESSION['firstname'] is missing or defaults to 'Guest' in home.php.

Code: register.php (Handles user signup and session creation)

session_start();

if (!isset($_SESSION['user_id'])) {
    header("Location: login_style.html");
    exit();
}

// Retrieve user input
$firstname = trim($_POST['firstname']);
$email = trim($_POST['email']);
$password = trim($_POST['password']);
$repeat_password = trim($_POST['repeat-password']);

if ($password !== $repeat_password) {
    die("Passwords do not match!");
}

// Database connection
$conn = pg_connect("host=localhost port=5432 dbname=webappDB user=postgres password=your_password");

if (!$conn) {
    die("Connection failed: " . pg_last_error());
}

// Hash password
$hashed_password = password_hash($password, PASSWORD_DEFAULT);

// SQL Insert (Possible issue here?)
$query = "INSERT INTO users (firstname, email, password) VALUES ($1, $2, $3)";
$result = pg_query_params($conn, $query, array($firstname, $email, $hashed_password));

if ($result) {
    // Retrieve user data
    $query = "SELECT user_id, firstname FROM users WHERE email = $1";
    $result = pg_query_params($conn, $query, array($email));

    if ($row = pg_fetch_assoc($result)) {
        $_SESSION['user_id'] = $row['user_id'];
        $_SESSION['firstname'] = $row['firstname']; // Set firstname in session
        $_SESSION['email'] = $email;
        $_SESSION['logged_in'] = true;

        var_dump($_SESSION); // Debugging
        header("Location: home.php");
        exit();
    } else {
        echo "Error fetching user data!";
    }
} else {
    echo "Registration Failed: " . pg_last_error($conn);
}

pg_close($conn);
?>

home.php (Displays session data)

session_start();

if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
    header('Location: login_style.php');
    exit();
}

$user_email = $_SESSION['email'] ?? 'Guest';
$user_name = $_SESSION['firstname'] ?? 'Guest'; // This does not work
?>

What I've Tried:

  1. Checked var_dump($_SESSION); in register.php → Shows firstname is set correctly.
  2. Checked for session start issues → session_start(); is at the top of both register.php and home.php.
  3. Tried reloading home.php → No change, still defaults to 'Guest'.
  4. Checked database query syntax → The insert and select queries seem correct.
Share Improve this question edited Feb 16 at 19:07 Dharman 33.4k27 gold badges101 silver badges147 bronze badges Recognized by PHP Collective asked Feb 16 at 18:53 MayMay 1 New contributor May is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 2
  • 1 I wonder about one thing. You add one user, so you have his data in variables. Why do you query for this data after adding him to the database? After all, you have them in variables, wouldn't it be better to use those from variables instead of redundant query? – LordF Commented Feb 16 at 21:33
  • 1 The SELECT is not needed at all, use RETURNING user_id in your INSERT to get the id in return. Fetch this result an you can use the id in your PHP code. – Frank Heikens Commented Feb 16 at 23:20
Add a comment  | 

1 Answer 1

Reset to default 0

Looking at your code snippets, I think the issue is with the data fetch from Postgres. Because the $_SESSION['firstname'] is set from the fetched row data while $_SESSION['email'] is from the earlier assigned variable in this line:

$email = trim($_POST['email']);

To correct it in the fetch instead of:

if ($row = pg_fetch_assoc($result)) {
        $_SESSION['user_id'] = $row['user_id'];
        $_SESSION['firstname'] = $row['firstname']; // Set firstname in session
        $_SESSION['email'] = $email;
        $_SESSION['logged_in'] = true;

        var_dump($_SESSION); // Debugging
        header("Location: home.php");
        exit();
    } else {
        echo "Error fetching user data!";
    }

You should use the while loop:

 while ($row = pg_fetch_assoc($result)) {
        $_SESSION['user_id'] = $row['user_id'];
        $_SESSION['firstname'] = $row['firstname']; // Set firstname in session
        $_SESSION['email'] = $email;
        $_SESSION['logged_in'] = true;
    }
发布评论

评论列表(0)

  1. 暂无评论