0

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>");
    }
}

 ?>
2
  • 1
    Might be because you're outputting before headers header('Location: ./dashboard.php');, for example your <?php include('header.php');?> you could place ob_start(); just above session_start(); and see if that fires it up. Plus I noticed you're using mysql_* functions (with no prepared statements) along with md5 to store passwords, not a good combination, considering that both are old technology and that md5 dates 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. Commented Jan 9, 2014 at 4:09
  • always start session at the top.. <?php session_start(); ?> just like programming student done Commented Jan 9, 2014 at 5:28

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I followed your advice and added ob_start();, as recommended by @Fred-ii- and the code started to work again.
Another Happy Ending then @SyedKamran Glad to hear it, cheers.
0

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');

Comments

0

Do session_start(); into your page first line

<?php
session_start();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.