header error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
frosty16
Forum Newbie
Posts: 19
Joined: Sat Jan 26, 2008 6:58 am

header error

Post 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
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Re: header error

Post 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.
frosty16
Forum Newbie
Posts: 19
Joined: Sat Jan 26, 2008 6:58 am

Re: header error

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: header error

Post 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.
frosty16
Forum Newbie
Posts: 19
Joined: Sat Jan 26, 2008 6:58 am

Re: header error

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: header error

Post 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.
frosty16
Forum Newbie
Posts: 19
Joined: Sat Jan 26, 2008 6:58 am

Re: header error

Post 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
Last edited by frosty16 on Sat Jan 26, 2008 10:36 am, edited 1 time in total.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: header error

Post 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.
frosty16
Forum Newbie
Posts: 19
Joined: Sat Jan 26, 2008 6:58 am

Re: header error

Post 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?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: header error

Post 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:
Post Reply