I am doing a project on two different servers, my code works perfectly on one server and refuses to work on the other.
The purpose of the code is for a user to login on the login.php page, and be redirected to the dashboard.php page, if their login credentials are correct. The header.php file simply contains information for the nav bar for different people logging in.
Please let me know where the error could be.
I'm not sure whether these are two distinct problems but both the Header redirect is not working, and neither are the session variables being stored. I made sure I didn't echo out anything before the header redirect.
Login.php
<?php include('header.php');?>
<?php
session_start();
$dbusername = $_SESSION['username'];
$dbfName = $_SESSION['fName'];
$dblName = $_SESSION['lName'];
$sessiontype = $_SESSION['type'];
if($dbusername && $dbfName && $dblName && $sessiontype){
header('Location: ./dashboard.php');
}
if(isset($_POST['login_button'])){
session_start();
$getuser = $_POST['username'];
$getpass = $_POST['password'];
$getpassmd5 = md5(md5($getpass));
if($getuser && $getpass){
require('connect.php');
$query1 = "SELECT * FROM students WHERE StudentNum='$getuser'";
$exequery1 = mysql_query($query1);
if(mysql_num_rows($exequery1) > 0){
while ($row = mysql_fetch_assoc($exequery1)){
$dbusername = $row['StudentNum'];
$dbpass = $row['password'];
$dbDOB = $row['DOB'];
$dbfName = $row['FirstName'];
$dblName = $row['LastName'];
}
if($dbpass){
if($getuser === $dbusername && $dbpass === $getpassmd5){
$_SESSION['username'] = $dbusername;
$_SESSION['fName'] = $dbfName;
$_SESSION['lName'] = $dblName;
$_SESSION['type'] = "student";
header('Location: ./dashboard.php');
}
else{
echo("<h4><center>You have entered incorrect login credentials</h4></center>");
}
}
else{
if($getuser === $dbusername && $getpass === $dbDOB){
$_SESSION['username'] = $dbusername;
$_SESSION['fName'] = $dbfName;
$_SESSION['lName'] = $dblName;
$_SESSION['type'] = "student";
header('Location: ./dashboard.php');
}
else{
echo("<h4><center>You have entered incorrect login credentials</h4></center>");
}
}
}
else{
$query2 = "SELECT * FROM teachers WHERE username='$getuser'";
$exequery2 = mysql_query($query2);
if(mysql_num_rows($exequery2) > 0){
while ($row = mysql_fetch_assoc($exequery2)){
$dbusername = $row['username'];
$dbpass = $row['password'];
$dbfName = $row['FirstName'];
$dblName = $row['LastName'];
$dbtype = $row['type'];
}
if($getuser === $dbusername && $dbpass === $getpassmd5){
$_SESSION['username'] = $dbusername;
$_SESSION['fName'] = $dbfName;
$_SESSION['lName'] = $dblName;
$_SESSION['type'] = $dbtype;
header('Location: ./dashboard.php');
}
else{
echo("<h4><center>You have entered incorrect login credentials</h4></center>");
}
}
else{
echo("<h4><center>You have entered login credentials that do not exist</center></h4>");
}
}
}
else{
echo("<h4><center>Please enter both a username and password</center></h4>");
}
}
?>
header('Location: ./dashboard.php');, for example your<?php include('header.php');?>you could placeob_start();just abovesession_start();and see if that fires it up. Plus I noticed you're usingmysql_*functions (with no prepared statements) along withmd5to store passwords, not a good combination, considering that both are old technology and thatmd5dates as far back as 1996. Nicely structured code, but it's old technology and is only a matter of time till your site gets hacked; sorry to say.