Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    headers already sent error.

    hi i am getting the following error when i log onto a php page i have made:
    Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\(removed)\Desktop\xampplite\htdocs\LoginForm.php:14) in C:\Documents and Settings\(removed)\Desktop\xampplite\htdocs\LoginForm.php on line 76

    code from LoginForm.php:
    PHP Code:
    <html>

    <head>
      <title>.:Login Page:.</title>
    </head>

    <body>

    <h1>Welcome to the login page!</h1>

    <!--the server [php_self] variable here allows us to post
    form data back to the same page-->

    <form action="<?=$_SERVER['PHP_SELF']?>"method="post">

    <p>Login: <input type="text" name="LoginName" size="30" maxlength="30"/></p>
    <p>Password: <input type="password" name="Pass" size="8" maxlength="8" /> </p>
    <p><input type="submit" name="submit" value="submit" /></p>
    </form>


    </body>
    </html>



    <?php
    //check for the submit variable and if it exists process the form



    //set $_POST variables here
    $username $_POST[LoginName];
    $pass $_POST[Pass];
    echo 
    'Welcome';


    //connect to mysql
    $con mysql_connect("localhost","root");


    //if the connection doesn't work then die with message
    if (!$con)
    {
      die(
    'could not connect;' mysql_error());
    }
      
      
    //select the database or die with message
    mysql_select_db(members,$con)
    or die(
    'could not find database' mysql_error());


    //create the query
    $query1 "SELECT UserId FROM logininfo WHERE (LoginName='$username' AND Pass='$pass')";


    //execute the query or die with message
    $result mysql_query($query1)
    or die (
    "Error in query" mysql_error());


    //set the variable $num to equal the number or rows returned
    $num mysql_num_rows($result);
    if(
    $username == NULL || $pass == NULL)
    {
      echo 
    'you need to fill in all fields';
    }
    elseif(
    $num !=&& $username !=NULL && $pass != NULL) {
      echo 
    'sorry you have the wrong login / password combination<br />';
      echo 
    'try again or...<br />';
      echo 
    'if you are not registered please go <a href="register.php">HERE</a>';
    }
    elseif(
    $num==1)
    {
    header("Location:site.php");
    }  
    ?>
    Last edited by Inigoesdr; 05-14-2008 at 09:32 PM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Just google the error, you'll get plenty of results, http://www.google.co.in/search?hl=en...s+already+sent

    (Instead of echoing those errors, just store them in an array and then echo them at the end)
    Last edited by abduraooft; 05-14-2008 at 09:06 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Apr 2008
    Posts
    26
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Your problem should be here:

    PHP Code:
    header("Location:site.php"); 
    Is this page being loaded by itself or being included in another page? Try another form of redirection and it will work.
    Last edited by pixencom; 05-14-2008 at 10:11 AM.
    Atlanta Web Design <- They do good work.

  • #4
    New Coder
    Join Date
    Mar 2008
    Location
    Somerset, England
    Posts
    93
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Simple solution, put the php code that comes after your </html> tag into a seperate file, e.g(inc_login.php). Remove the echo 'Welcome'; line. And instead of echoing out errors, store them in variables and then echo these variables on the html page.

    Include this file at the top of your page before the <html> tag.

    e.g.
    PHP Code:
    <?php require_once('inc_login.php'); ?>
    <html>
    ...
    </html>
    Voilla everything now works.

    This is because you cannot alter header information once output has already started. By including the php at the begining of the page before the <html> tag header information can now be altered.

    On a side note it's good practice to seperate your php code from your html, it makes for much easier editing later on. I reccomend setting up a folder called 'includes' or something along those lines that has all your php files in it. Then in your html pages include the required php files from that folder.
    Last edited by RMcLeod; 05-14-2008 at 03:36 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •