Page 1 of 1

Warning: Cannot modify header information - headers already

Posted: Wed Sep 02, 2009 2:14 am
by phpfan
hi
i need help for this program........... i have created a table members with column names id, username, password.
in the database there is one record and after the user gives the username and password if the result matches the the page should redirect.

login.php

Code: Select all

<?php
    session_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <center>
        <form action="" method="post" name ="subform">
                <table width="250" align="center" border="0" cellpadding="2" cellspacing="1">
                    <tr>
                        <td>
                        Username:&nbsp&nbsp<input type="text" name="user" /><br>
                        </td>
                    </tr>
                    <tr>
                        <td>
                        Password:&nbsp&nbsp&nbsp&nbsp&nbsp<input type="password" name="pass" /><br>
                        </td>
                    </tr>
                    <tr>
                        <td>
                         &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type="submit" name="submit" value="Submit"/>
                        </td>
                    </tr>
                 </table>
            </form>
        </center>
        <?php
 
            $dblogin = "root";
            $dbpwd = "";
            $host = "localhost";
 
            $dbconnection = mysql_connect($host,$dblogin,$dbpwd) or die ('error'.mysql_error());
 
            $selectdb = mysql_select_db("example", $dbconnection);
 
            if(!$selectdb)
            {
                echo "error in connection string".mysql_error();
            }
            else
            {
                echo "Connection Successfull";
            }
            echo "<br/>";
 
            $username = $_POST['user'];
            $password = $_POST['pass'];
 
            $sql="SELECT * FROM members WHERE id='' and username='$username' and password='$password'";
 
            $result = mysql_query($sql);
 
// This counts to see how many rows were found, there should be no more than 1
            $count = mysql_num_rows($result);
 
            if($count==1)
            {
                // Register $myusername, and redirect to file "login_success.php"
                //session_start();
                $_SESSION["logged"] = 1;
                header("location:index.php");
             }
             else 
             {
                $_SESSION["logged"] = 0;
                header("location:login.php");
 
             }   
 
        ?>
    </body>
</html>
index.php

Code: Select all

<?php
    //session_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        echo "welcome".$_SESSION['username'];
        ?>
        <a href="logout.php/">LogOut!</a>
    </body>
</html>
kindly help me on this issue,

Re: Warning: Cannot modify header information - headers already

Posted: Wed Sep 02, 2009 2:52 am
by Mark Baker
The http response headers are sent by the first line of your script that generates any output, which is line 4 (your doctype declaration). Once sent, they cannot be modified or resent, which is what you're trying to do with the header() statements in lines 68 and 73.
What you need to do is reorder your script in such a way that the PHP code with the header() statements is executed before the HTML output

Re: Warning: Cannot modify header information - headers already

Posted: Wed Sep 02, 2009 3:46 am
by phpfan
Thanks for the reply it is appreciated, i have removed the else part now i am having no errors but i have a problem now, when i enter username = henry & password = henry same the records in the database, when i click submit nothing is happening out there just the page is refreshing and the values are refreshed, how to solve this