-4

Possible Duplicate:
Warning: Cannot modify header information - headers already sent
Headers already sent by PHP

Error: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\GameHutWebsite\data\connection.php:1) in C:\xampp\htdocs\GameHutWebsite\login.php on line 21

I am getting this error when the user enters the correct username and password in the login screen to send the user in the home page. This is the code:

<?php
if ($_POST)
    {
        $dsLogin = $dataobj->validateLogin($_POST["txtUsername"],$_POST["txtPassword"]);

        if (mysql_num_rows($dsLogin))
        {
            $dsUserDetails = mysql_fetch_array($dsLogin);
            $_SESSION["UserEmail"] =  $dsUserDetails["UserEmail"];
            $_SESSION["Username"] =  $dsUserDetails["Username"];
            $_SESSION["UserTypeId"] =  $dsUserDetails["UserTypeId"];
            header ("Location: index.php");
        }
        else
        {
            $msg = "Invalid Username and Password!";
        }
    }
?>

Connection Class:

<?php
class connection
{   
    function connect($sql)
    {
        $server = 'localhost';
        $myDB = 'gamehutdb'; 

        //connection to the database
        $dbhandle = mysql_connect($server, 'root', "")
            or die("Couldn't connect to SQL Server $server"); 
        //select a database to work with
        $selected = mysql_select_db($myDB)
            or die("Couldn't open database $myDB"); 

        //execute the SQL query and return records
        $result = mysql_query($sql);

        //return result set
        return $result;
        //close the connection
        mysql_close($dbhandle); 
    }
}
?>
Community
  • 1
  • 1

2 Answers2

4

You have used

header ("Location: index.php");

AFTER sending something to the client (ie, using echo or print), make sure that there is nothing sent to the client before that code.

Another reason might be a BOM problem.

Community
  • 1
  • 1
Adi
  • 5,089
  • 6
  • 33
  • 47
2

This is probably due to the fact that your connection class is emitting some kind of output

Comment out the "header" line and run your script. If you get some output, then that's the reason.

You should conditionally launch the "header" line based on whether you get an error or not, at least while you troubleshoot your problem.

Raul Marengo
  • 2,287
  • 1
  • 15
  • 10
Assaf Karmon
  • 915
  • 1
  • 10
  • 23