Hey Guys/Gals
Benn awhile - so I'm confused on what's happening here. session_start() being called on every page, index.php, registration.php and server.php aswell as login.php. Now when I go too login - if I edit the code to echo the $_SESSION['username'] it works fine - gives me what I expect... I set the $_SESSION['username'] = $username and very next line header('Location: index.php'); this is where the issues is it's loosing the session data and I can't figure out why... so here are the two files if someone can point me in the right direction..
index.php
<?php ob_start(); session_start();
if (!isset($_SESSION['username'])) {
echo print_r($_SESSION);
}else {
echo print_r($_SESSION);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="styles/style.css">
</head>
<body>
<div class="header">
<h2>Home Page</h2>
</div>
<div class="content">
<!-- notification message -->
<div class="error success" >
<h3>
<!-- there was error code display here -->
</h3>
</div>
<!-- logged in user information -->
</div>
</body>
</html>
server.php
<?php ob_start(); session_start();
ini_set('display_errors', '1'); ini_set('html_errors', '1'); error_reporting(E_ALL);
//make connection with dba_close
$username = "";
$email = "";
$errors = array();
$host = "localhost";
$db_username = "";
$db_password = "";
$db = "";
$conn = mysqli_connect($host,$db_username,$db_password,$db);
if (mysqli_connect_errno()) {
echo "Connection Failed... please contact support". mysqli_connect_error();
}
//login user form details
if (isset($_POST['login_user'])) {
$username = $_POST['username'];
//login for form initiated
$user = "SELECT * FROM `users` WHERE `username` = '".$username."'";
$action = mysqli_query($conn,$user);
while ($row = mysqli_fetch_assoc($action)) {
if (mysqli_num_rows($action) === 1) {
$_SESSION['username'] = $username;
// echo "<br>";
// echo $_SESSION['username']; <-- this works echo's the username, if I remove header location redirect
header('Location: index.php');
exit();
}else {
echo "Username not found";
}
}
}
?>