Page 1 of 1

header error

Posted: Sat Jan 26, 2008 7:03 am
by frosty16
Heya everyone,

Im new to the forum and am in the process of teaching myself php/mysql. I have got the following code:

Code: Select all

<html>
<head>
<title>The Playhouse Company Admin System</title>
</head>
<body>
<p align="center">
  <?php
    include("Login.inc");
 
    $cxn = mysqli_connect($host, $user, $password, $dbname) 
            or die ("Couldn't connect to server.");    
 
     $sql = "SELECT * FROM user
             WHERE username='$_POST[username]'";     
     $result = mysqli_query($cxn,$sql)
               or die("Couldn't execute query.");      
     $num = mysqli_num_rows($result);                  
     if ($num > 0)  // login name was found            
     {
        $sql = "SELECT * FROM user 
                WHERE username='$_POST[username]'
                AND password=('$_POST[password]')";
        $result2 = mysqli_query($cxn,$sql)
                   or die("Couldn't execute query 2.");
        $num2 = mysqli_num_rows($result2);
        if ($num2 > 0)  // password is correct         
        {
           $_SESSION['auth']="yes";                    
           $username=$_POST['username']; 
           $_SESSION['username'] = $username;            
           header("Location: index.php");        
        }
        else    // password is not correct             
        {
           $message="The Login Name, '$_POST[username]' 
                     exists, but you have not entered the 
                     correct password! Please try again.<br>";
           include("login_form.inc");                 
        } 
     }                                                 
     elseif ($num == 0)  // login name not found       
     {   
        $message = "The Login Name you entered does not 
                    exist! Please try again.<br>";
        include("login_form.inc");
     }
?>
</p>
</body>
</html> 
However it is bringing up the following error message:

"Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\theplayhousecompany\Login.php:7) in C:\AppServ\www\theplayhousecompany\Login.php on line 31"

Line 31 = header("Location: index.php");

Can anyone tell me what i am doing wrong?

Thank you in advance

Frosty

Re: header error

Posted: Sat Jan 26, 2008 7:41 am
by jamiel
All header() commands need to be before any output is sent to the browser. So that code needs to be before your <html> tag.

Re: header error

Posted: Sat Jan 26, 2008 7:47 am
by frosty16
What do you mean?
Sorry i don't really understand as if i put it before <html> then it displays 'header("Location: index.php")' on the logon page rather than displaying the index.php page if the logon details are correct

Re: header error

Posted: Sat Jan 26, 2008 8:38 am
by JAM
frosty16 wrote:What do you mean?
Sorry i don't really understand as if i put it before <html> then it displays 'header("Location: index.php")' on the logon page rather than displaying the index.php page if the logon details are correct

Code: Select all

<html>
<head>
<title>The Playhouse Company Admin System</title>
</head>
<body>
<p align="center">
  <?php
    include("Login.inc");
The Login.inc contains a header(), but before including that script/page, (and by that calling that function) you are sending html and that youre not allowed to do.
All header() calls should come first att all times in scripts.

Re: header error

Posted: Sat Jan 26, 2008 9:29 am
by frosty16
ok so where would i need to put the Header bit then as if i don't call what is in login.inc i just code it in then it still gives me the same error.

Login.inc =

Code: Select all

<?php
$host="localhost";
$user="root";
$password = "Password";
$dbname = "theplayhousecompany";
?>
Basically what i am trying to do is if the login details are correct then display the page called index.php. I'm not even sure if header(Location: index.php") is the correct thing to use for this.

Re: header error

Posted: Sat Jan 26, 2008 10:13 am
by JAM
I must say sorry, cause I misread your post! :oops:

The login.inc does indeed not contain any header(), it comes further down in the script. What you need to do tho, is to do all checks before the <html> is shown. An (simplified) example;

Code: Select all

<?php
    if ($user != 'OK') {
        header('Location: http://www.google.com/');
    } 
?>
<html>
    <head>
    [...]
The way you are doing it now, youre displaying part of html, then check if everything's okay, if not, you try to header() the user out.

Re: header error

Posted: Sat Jan 26, 2008 10:27 am
by frosty16
do you mean so that it is like this:

Code: Select all

<?php
    include("Login.inc");
 
    $cxn = mysqli_connect($host, $user, $password, $dbname) 
            or die ("Couldn't connect to server.");   
 
     $sql = "SELECT * FROM user
             WHERE username='$_POST[username]'";     
     $result = mysqli_query($cxn,$sql)
               or die("Couldn't execute query.");      
     $num = mysqli_num_rows($result);                  
     if ($num > 0)  // login name was found            
     {
        $sql = "SELECT * FROM user 
                WHERE username='$_POST[username]'
                AND password=('$_POST[password]')";
        $result2 = mysqli_query($cxn,$sql)
                   or die("Couldn't execute query 2.");
        $num2 = mysqli_num_rows($result2);
        if ($num2 > 0)  // password is correct         
        {
           $_SESSION['auth']="yes";                    
           $username=$_POST['username']; 
           $_SESSION['username'] = $username;                  
           header("Location: index.php");        
        }
        else    // password is not correct             
        {
           $message="The Login Name, '$_POST[username]' 
                     exists, but you have not entered the 
                     correct password! Please try again.<br>";
           include("login_form.inc");                  
        } 
     }                                                 
     elseif ($num == 0)  // login name not found       
     {   
        $message = "The Login Name you entered does not 
                    exist! Please try again.<br>";
        include("login_form.inc");
     }
?>
<html>
<head>
<title>The Playhouse Company Admin System</title>
</head>
<body>
<p align="center">
 
</p>
</body>
</html>
Header("Location: Login.php") is on line 25 now i believe.

Is that correct as that till isnt working. :?

frosty

Re: header error

Posted: Sat Jan 26, 2008 10:34 am
by JAM
Any errors? Try this aswell;

Code: Select all

<?php
    error_reporting(E_ALL);
    include("Login.inc");
    $cxn = mysqli_connect($host, $user, $password, $dbname) or die ("Couldn't connect to server.");
    $sql = "SELECT * FROM user WHERE username='$_POST[username]'";
    $result = mysqli_query($cxn,$sql) or die("Couldn't execute query.");
    $num = mysqli_num_rows($result);
    if ($num > 0) {
        // login name was found
        $sql = "SELECT * FROM user WHERE username='$_POST[username]' AND password=('$_POST[password]')";
        $result2 = mysqli_query($cxn,$sql) or die("Couldn't execute query 2.");
        $num2 = mysqli_num_rows($result2);
        if ($num2 > 0) {
            // password is correct
           $_SESSION['auth']="yes";
           $username=$_POST['username'];
           $_SESSION['username'] = $username;
           header("Location: index.php");
        } else {
            // password is not correct
           $message="The Login Name, '$_POST[username]' exists, but you have not entered the correct password! Please try again.<br>";
           $page = "login_form.inc";
        }
    } elseif ($num == 0) {
        // login name not found
        $message = "The Login Name you entered does not exist! Please try again.<br>";
        $page = "login_form.inc";
    }
?>
<html>
<head>
<title>The Playhouse Company Admin System</title>
</head>
<body>
<p align="center">
<?php
    if (!empty($page)) {
        require $page;
    }
?>
</p>
</body>
</html>
What I did was to assign pages to variables instead of includes() as it now producing bad html. I later use them in the actual page. I also added error_reporting at the top to help you understand any errors.

Re: header error

Posted: Sat Jan 26, 2008 10:45 am
by frosty16
Brilliant that is working perfectly now however it is displaying the following message at the top of the login page:

"Notice: Undefined index: username in C:\AppServ\www\theplayhousecompany\Login.php on line 5"

Is there any way i can get rid of this?

Re: header error

Posted: Sat Jan 26, 2008 10:49 am
by JAM
As noticed, the error lies in this row;

Code: Select all

$sql = "SELECT * FROM user WHERE username='$_POST[username]'";
...and thats cause $_POST is used when posting forms, and you are likely viewing the page directly.

You need to add something similiar as;

Code: Select all

if (!empty($_POST['username'])) {
    $sql = "SELECT * FROM user WHERE username='$_POST[username]'";
// and more code here
I'm not rewriting your page, you should try to learn and understand what we are doing here. :wink: