182x Posted July 12, 2007 Share Posted July 12, 2007 Hey guys, I get the following error message when using the code below: Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\a\processLogin.php:10) in C:\xampp\htdocs\a\processLogin.php on line 48 I was just wondering how to fix it? Thanks. line 48: case 1: header("location:admin.html"); break; <?php session_start(); include ('db.php'); $li = db(''); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Login</title> <style type="text/css"> <!-- .style1 { font-size: 29px; font-family:Helvetica, sans-serif; } --> </style> </head> <body bgcolor="#CCCCCC"> <?php $u = $_POST['u']; $p = $_POST['p']; $s = "SELECT * FROM `u` WHERE `u` ='$u'"; $r = mysql_query($s,li) or die(mysql_error()); if(mysql_num_rows($r) > 0) { $s2 = "SELECT * FROM `u` WHERE `u` ='$u' AND `p` ='$p'"; $r2 = mysql_query($s2,li) or die(mysql_error()); if(mysql_num_rows($r2) > 0){ $r = mysql_fetch_assoc($r2); $l = $r[al]; switch($l) { case 1: header("location:test.html"); break; case 2: header("location:test1.html"); break; exit(); } } else { echo '<p align="center"><span class="style1">incorrect.</span></p>'; echo '<p align="center"><a href="index.php" class="style1">Back</a></p>'; } } else { echo '<p align="center"><span class="style1">error.</span></p>'; echo '<p align="center"><a href="index.php" class="style1">Back</a></p>'; } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
trq Posted July 12, 2007 Share Posted July 12, 2007 did you read the big sticky at the top of this board? Quote Link to comment Share on other sites More sharing options...
182x Posted July 12, 2007 Author Share Posted July 12, 2007 Yeah I did although still I can't figure out how to make it work and keep the HTML. Quote Link to comment Share on other sites More sharing options...
trq Posted July 12, 2007 Share Posted July 12, 2007 still I can't figure out how to make it work and keep the HTML. You can't> You need to fix your logic so that the html is only outputted after any calls to header. You don't need any html prior to a redirect anyway... makes little sense. One hack would be to wrap all your code in... <?php ob_start(); // code here. ob_end_flush(); ?> But as I said... that is a hack. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 12, 2007 Share Posted July 12, 2007 Move all PHP processing before the HTML, example: <?php session_start(); include ('db.php'); $li = db(''); $u = $_POST['u']; $p = $_POST['p']; $s = "SELECT * FROM `u` WHERE `u` ='$u'"; $r = mysql_query($s, $li) or die(mysql_error()); if(mysql_num_rows($r) > 0) { $s2 = "SELECT * FROM `u` WHERE `u` ='$u' AND `p` ='$p'"; $r2 = mysql_query($s2, $li) or die(mysql_error()); if(mysql_num_rows($r2) > 0) { $r = mysql_fetch_assoc($r2); $l = $r['al']; switch($l) { case 1: header("location:test.html"); break; case 2: header("location:test1.html"); break; } } else { $msg = '<p align="center"><span class="style1">incorrect.</span></p>'; $msg .= '<p align="center"><a href="index.php" class="style1">Back</a></p>'; } } else { $msg = '<p align="center"><span class="style1">error.</span></p>'; $msg .= '<p align="center"><a href="index.php" class="style1">Back</a></p>'; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Login</title> <style type="text/css"> <!-- .style1 { font-size: 29px; font-family:Helvetica, sans-serif; } --> </style> </head> <body bgcolor="#CCCCCC"> <?php echo $msg; ?> </body> </html> Or another method is to change these lines: case 1: header("location:test.html"); break; case 2: header("location:test1.html"); break; To this: case 1: redirect_to("test.html"); break; case 2: redirect_to("test1.html"); break; Then add this code: function redirect_to($location) { header('Location: ' . $location); } At the the top of the page, After the following: session_start(); include ('db.php'); $li = db(''); Quote Link to comment Share on other sites More sharing options...
182x Posted July 12, 2007 Author Share Posted July 12, 2007 Thats awesome thanks for all the help guys. I didn't realise that if the php code was put before the html tags that it would still keep the html formatting. Is that also a hack or is that just normal practice? Thanks again. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 12, 2007 Share Posted July 12, 2007 No that is just normal practice. No form of output, eg: HTML, whitespace or any form of text should be outputted before a call to any function that sends/requests headers eg: setcookie, session_start, header etc. Output is formed when you either tell php to echo/print a string within the php tags or anything outside of the php tags. Also I have modified my post with another suggestion. Quote Link to comment Share on other sites More sharing options...
182x Posted July 12, 2007 Author Share Posted July 12, 2007 Thanks for the alternate option. How does the php output keep the html formatting when it is called before the html? Is it to do with how it is parsed by the browser? Quote Link to comment Share on other sites More sharing options...
trq Posted July 12, 2007 Share Posted July 12, 2007 How does the php output keep the html formatting when it is called before the html? You simply store any output produced by your logic within variables, then, echo those variables within the html. eg; <body bgcolor="#CCCCCC"> <?php echo $msg; ?> </body> Quote Link to comment Share on other sites More sharing options...
182x Posted July 12, 2007 Author Share Posted July 12, 2007 Ah yes I see that makes a lot more sense. I left the echo in by mistake instead of storing it as a variable and it still displayed properly that was confusing me. I still am not sure why it was displaying properly when it wasn't stored in variables with the html formatting. Thanks again. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 12, 2007 Share Posted July 12, 2007 Which code are you referring to? Your old code (posted in your first post) or my code in this post? If you mean my code and you are changing $msg = to echo then the code will still work as you have outputted the HTML after you have called the header function. It is ok to send output after a call to header but it is not ok if you send output before a call to header. Quote Link to comment Share on other sites More sharing options...
182x Posted July 14, 2007 Author Share Posted July 14, 2007 Thanks, does this apply to all cases or is t here any instance where the HTML formatting would be lost? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 15, 2007 Share Posted July 15, 2007 Thanks, does this apply to all cases or is t here any instance where the HTML formatting would be lost? Not sure what you mean, can you post code examples or explain your question in more detail. The general rule is to not place any form of output before a call to the header function (or any function that requires headers, eg: session_start, setcookie and header). However it is ok to output anything after a call to the header function (or any function that requires headers). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.