Reputation: 21
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>");
}
}
?>
Upvotes: 0
Views: 1442
Reputation: 4620
Do session_start();
into your page first line
<?php
session_start();
Upvotes: 0
Reputation: 632
Sounds like an Apache server or PHP configuration issue. On both servers, run a script with:
phpinfo();
Compare them for discrepancies. Also, check the PHP version, loaded extensions, and configurations in the .ini. It could be Apache httpd.conf, but I'm guessing on it being a php.ini issue or a PHP version issue.
Recommendation: create an autoloader to be a PHP include on line 1 of every file. Autoload your session and db and constants in there. This will ensure the session is loaded prior to outputting HTML, which seems to be where others are seeing an issue.
line 1: require_once('config.php');
Upvotes: 0
Reputation: 23958
Firstly, You should not have different <?php
and ?>
tags.
As its counted as a space.
<?php include('header.php');?>
<?php
session_start();
Should be:
<?php
session_start();
include('header.php');
Its adding a space in the file, hence redirection is not taking place.
Upvotes: 3