Jump to content

"headers already sent by" problem


Dave Stewart

Recommended Posts

Hi everyone,

please bear with me as I am a novice!

 

I eventually got my login system to work but am trying to send the successfull login to a members page & am getting

"Warning: Cannot modify header information - headers already sent by (output started at..."

It claims that the headers were already set in the same PHP. I cannot find any header info before the line I'm using so am stuck. Anyone able to help me out here?

Thanks.

 

The coding is:

 

<?php$name = $_POST['name'];$password = $_POST['password'];if ($name && $password) { $connect = mysql_connect("--", "--", "--") or die('Connection to database failed'); mysql_select_db("--") or die ("Couldnt find database"); $sql = mysql_query("SELECT * FROM <the table> WHERE name = '$name'"); $numrows = mysql_num_rows($sql); if($numrows !=0) { while($row = mysql_fetch_assoc($sql)) { $dbname = $row['name']; $dbpassword = $row['password']; } if($name==$dbname && $password==$dbpassword) { $_SESSION['name']=$dbname; header("Location: --.html"); } else echo ("Incorrect data entered"); } else echo "<center>your login was unsuccessful so please try again"; }?>

 

It seems to be working except the header line. :(

Link to comment
Share on other sites

Seeing echo "<center>your login was unsuccessful so please try again"; suggests to me that this code would be placed and shown in the body tag of the page, if this is the case then this is the problem the header("Location: --.html"); MUST be at the very top of the page BEFORE any html.

Link to comment
Share on other sites

Seeing echo "<center>your login was unsuccessful so please try again"; suggests to me that this code would be placed and shown in the body tag of the page, if this is the case then this is the problem the header("Location: --.html"); MUST be at the very top of the page BEFORE any html.

 

Thanks, and yes it is. But surely the login code needs to run first? Is there a way to redirect the page once the login script has proved successful?

Link to comment
Share on other sites

Similar to this

<?php$name = $_POST['name'];$password = $_POST['password'];if ($name && $password) { $connect = mysql_connect("--", "--", "--") or die('Connection to database failed'); mysql_select_db("--") or die ("Couldnt find database"); $sql = mysql_query("SELECT * FROM <the table> WHERE name = '$name'"); $numrows = mysql_num_rows($sql); if($numrows !=0)   {   while($row = mysql_fetch_assoc($sql))     {     $dbname = $row['name'];         $dbpassword = $row['password'];     }   if($name==$dbname && $password==$dbpassword)     {     $_SESSION['name']=$dbname;     header("Location: --.html");     }  else{?> <!DOCTYPE html><html>    <head>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">        <meta name="viewport" id="viewport" content="target-densitydpi=high-dpi,initial-scale=1.0,user-scalable=no" />        <title>Document Title</title>        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>        <script  type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>        <script type="text/javascript">        </script>        <style type="text/css">        </style>    </head>    <body><?php// you don't really need to php echo this, just plain html would do    echo ("<p>Incorrect data entered</p>");   echo "<center>your login was unsuccessful so please try again</center>"; ?>    </body></html><?php}}?>

Like i said, before html code, not! php coding

Edited by dsonesuk
Link to comment
Share on other sites

Yes thanks, that makes sense, but there must be a way to... hmm, thnking out loud here... if I put the login form into the page I suppose I can refer the results to the php coding inside the head yes? I'm not quite sure how to do that but presume all login pages do so. I'll go and try things out. Thanks for your help.

Link to comment
Share on other sites

Yesterday.

 

I transferred all the php coding up into the header & rearranged my pages, but am still having the same trouble. The only difference is that the erros come at the top before the page loads.

 

I won't repost all the coding now that I've rearranged it but can easily do if that will help at all. If you'd rather just look at the site to figure out where i'm going wrong, please do. It's at www.daveweb.in Please don't laugh at it as it's just a place for me to try to learn what to do, although I'm obvioulsy stuck on this bit!

 

Thanks for any help you can give me.

Dave

Link to comment
Share on other sites

Stop press!!!! It worked! I realised after my last post to you that I had put the coding inside the head, not before the first tag as you had advised. I need to change a few bits of the ite but that worked as I'd hoped. Thank you LOADS for your advice. Greatly appreciated, and very wose. Thanks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...