Hi All
I’ve created the login system and all is good, however i’m trying to echo the logged in users name in the header with no joy. I’ve tried multiple different ways from various sites around google but i can’t seem to get it functioning. Please can someone take a look and let me know where i’m going wrong…
<?php
//Login Page
// 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){
if ($_SESSION["user_level"]==99){
header("location: admin/index.php");
exit;
}
else {
header("location: dealer/index.php");
exit;
}
}
// Include config file
require_once "includes/db_conn.php";
// Define variables and initialize with empty values
$email = $password = "";
$email_err = $password_err = "";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Check if username is empty
if(empty(trim($_POST["email"]))){
$email_err = "Please enter email.";
} else{
$email = trim($_POST["email"]);
}
// Check if password is empty
if(empty(trim($_POST["password"]))){
$password_err = "Please enter your password.";
} else{
$password = trim($_POST["password"]);
}
// Validate credentials
if(empty($email_err) && empty($password_err)){
// Prepare a select statement
$sql = "SELECT id, fname, user_level, email, password FROM dealerenq WHERE email = ? AND status = 'Approved'";
if($stmt = $con->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bind_param("s", $param_email);
// Set parameters
$param_email = $email;
// Attempt to execute the prepared statement
if($stmt->execute()){
// Store result
$stmt->store_result();
// Check if username exists, if yes then verify password
if($stmt->num_rows == 1){
// Bind result variables
$stmt->bind_result($user_id, $userlevel, $fname, $email, $hashed_password);
if($stmt->fetch()){
if(password_verify($password, $hashed_password)){
// Password is correct, so start a new session
// Store data in session variables
$_SESSION["loggedin"] = true;
$_SESSION["user_id"] = $user_id;
$_SESSION["email"] = $email;
$_SESSION["user_level"] = $userlevel;
$_SESSION["fname"] = $fname;
if ($_SESSION["user_level"]!= 99){
header("location: dealer/index.php");
exit;
}
else if ($_SESSION["user_level"] == 99){
header("location: /admin/index.php");
exit;}
} 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
$email_err = "No account found with that email or account awaiting approval.";
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
$stmt->close();
}
// Close connection
$con->close();
}
?>
On the following page, please can you suggest how i would correctly call the session variable for $fname and echo this out elsewhere on the page.
<?php
// In header of dealer page //
// Initialize the session
session_start();
// Check if the user is logged in, if not then redirect him to login page
if(isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] == true){
if ($_SESSION["user_level"] == 99){
header("location: /admin/index.php");
exit;
}
else if ($_SESSION["user_level"] == 1){
//user authenticated - ok to show page
}
}
else {
header("location: /login.php?msg=ok&err=not_logged_in");
exit;
}
?>
Thanks in advance all.