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:
- Checked var_dump($_SESSION); in register.php → Shows firstname is set correctly.
- Checked for session start issues → session_start(); is at the top of both register.php and home.php.
- Tried reloading home.php → No change, still defaults to 'Guest'.
- 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:
- Checked var_dump($_SESSION); in register.php → Shows firstname is set correctly.
- Checked for session start issues → session_start(); is at the top of both register.php and home.php.
- Tried reloading home.php → No change, still defaults to 'Guest'.
- Checked database query syntax → The insert and select queries seem correct.
1 Answer
Reset to default 0Looking 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;
}
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