DanielHardy Posted November 14, 2009 Share Posted November 14, 2009 Hi All, I am getting the following error message in a script that was previously working fine. You can see it in action at http://mi-linux.wlv.ac.uk/~0607197/login.php I had the same problem on another page that was solved by removing white space before and after the php tags. This has not worked on this page. Here is the code <LINK rel="stylesheet" href="regal.css" type="text/css"> <script src="IEmarginFix.js" type="text/javascript"></script> </head> <BODY> <div id="mainsite" ;text-align:center;"> <div id="toppic"><div id="topgap" style="height:70px;"> </div><div id="nav"> <div id="navgapper"> <div class="clean22" style="padding-top:7px;padding-left:5px; font-weight:bold;;height:30px;color:FFFFFF;"> <font color="#000000"> <?php error_reporting (E_ALL); if(isset($_COOKIE['ID_my_site'])){ echo " Hello "; echo $_COOKIE['ID_my_site']; echo '<a href=logout.php><b> Logout</b></a>'."\n";} else { echo "Not Currently Signed In"; } ?> </font> </div> </div> <div class="nav1"> <a href="regal1.php">Home</a> </div> <div class="nav1"> <a href="regal2.php">Events</a> </div> <div class="nav1"> <a href="regal3.php">Prices</a> </div> <div class="nav2"> <a href="book1.php">Book</a> </div> <div class="nav1"> <a href="mailto:[email protected]">Contact Us</a> </div> </div> </div><?php // Connects to your Database mysql_connect("localhost", "0607197", "12345") or die(mysql_error()); mysql_select_db("db0607197") or die(mysql_error()); //Checks if there is a login cookie if(isset($_COOKIE['ID_my_site'])) //if there is, it logs you in and directes you to the members page { $username = $_COOKIE['ID_my_site']; $pass = $_COOKIE['Key_my_site']; $check = mysql_query("SELECT * FROM Users WHERE username = '$username'")or die(mysql_error()); while($info = mysql_fetch_array( $check )) { if ($pass != $info['password']) { } else { header("Location: book1.php"); } } } //if the login form is submitted if (isset($_POST['submit'])) { // if form has been submitted // makes sure they filled it in if(!$_POST['username'] | !$_POST['pass'] | !$_POST['pass'] ) { die('<h2><b>You did not fill in a required field.</b><a href="login.php"><font color="#b9c059">Back</a></font></h2>'); } // checks it against the database if (!get_magic_quotes_gpc()) { $_POST['email'] = addslashes($_POST['email']); } $check = mysql_query("SELECT * FROM Users WHERE username = '".$_POST['username']."'")or die(mysql_error()); //Gives error if user dosen't exist $check2 = mysql_num_rows($check); if ($check2 == 0) { die('<h2>No Such Account. <a href=register.php><font color="#b9c059">Click Here to Register</a></font></h2>'); } while($info = mysql_fetch_array( $check )) { $_POST['pass'] = stripslashes($_POST['pass']); $info['password'] = stripslashes($info['password']); $_POST['pass'] = md5($_POST['pass']); //gives error if the password is wrong if ($_POST['pass'] != $info['password']) { die('<h2> Incorrect password, please <a href=login.php><font color="#b9c059">Try again.</a></font>'); } else { // if login is ok then we add a cookie $_POST['username'] = stripslashes($_POST['username']); $hour = time() + 3600; setcookie(ID_my_site, $_POST['username'], $hour); setcookie(Key_my_site, $_POST['pass'], $hour); //then redirect them to the members area header("Location: book1.php"); } } } // if they are not logged in ?> <div class="clean22" style="text-align:center;"> <div id="regform" style="padding-top:15px;"> <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <!-- Form To Capture Login Data --> <center><table border="0" class="centeredtable" > <tr ><td colspan="2"><h1>Login</h1></td></tr> <tr ><td>Username:</td><td> <input type="text" name="username" maxlength="40"> </td></tr> <tr ><td>Password:</td><td> <input type="password" name="pass" maxlength="50"> </td></tr> <tr ><td colspan="2" > <input type="submit" name="submit" value="Login"> </td > </tr> </table> </form> <p> Not Registered? <a href="register.php"><b><font color="#b9c059">Join</a></b></font> <p><a href="adminlogin.php"><font color="#b9c059">Admins</font></a> </div> </div> </div> I have included the HTML as well incase there are issues here. Your help is, as always, greatly appreciated. Thanks Dan Link to comment https://forums.phpfreaks.com/topic/181495-solved-warning-cannot-modify-header-information-headers-already-sent-by/ Share on other sites More sharing options...
rarebit Posted November 14, 2009 Share Posted November 14, 2009 Certain actions require that no output is sent before them: header("Location: book1.php"); and setcookie(ID_my_site, $_POST['username'], $hour); setcookie(Key_my_site, $_POST['pass'], $hour); //then redirect them to the members area header("Location: book1.php"); All of the above fall into this category. You output data and then attempt to resend the header. ! Actually (still early) the cookies might not fall into this category... But 'header()' does for certain... Link to comment https://forums.phpfreaks.com/topic/181495-solved-warning-cannot-modify-header-information-headers-already-sent-by/#findComment-957396 Share on other sites More sharing options...
DanielHardy Posted November 14, 2009 Author Share Posted November 14, 2009 Ideas on how to solve it without comprimising the functionality? Never come accross this before. Weird how it worked before though is it not? Thanks Link to comment https://forums.phpfreaks.com/topic/181495-solved-warning-cannot-modify-header-information-headers-already-sent-by/#findComment-957401 Share on other sites More sharing options...
rarebit Posted November 14, 2009 Share Posted November 14, 2009 There's various ways, all depending upon how you code... Some people use a set set of buffering functions, see around here: http://uk2.php.net/manual/en/function.ob-start.php Me I code so that header critical stuff is processed prior to any output, even if it means an extra variable. Link to comment https://forums.phpfreaks.com/topic/181495-solved-warning-cannot-modify-header-information-headers-already-sent-by/#findComment-957404 Share on other sites More sharing options...
mga_ka_php Posted November 14, 2009 Share Posted November 14, 2009 put your code with header("Location: book1.php"); at the top. you already had declared header. or replace header("Location: book1.php"); with echo '<META http-equiv="refresh" content="1;URL=page.php">'; Link to comment https://forums.phpfreaks.com/topic/181495-solved-warning-cannot-modify-header-information-headers-already-sent-by/#findComment-957405 Share on other sites More sharing options...
DanielHardy Posted November 14, 2009 Author Share Posted November 14, 2009 Using the Meta tags didn't work, interesting how you can use these in such a way though What piece of code are you suggesting I move exactly? Link to comment https://forums.phpfreaks.com/topic/181495-solved-warning-cannot-modify-header-information-headers-already-sent-by/#findComment-957423 Share on other sites More sharing options...
rarebit Posted November 14, 2009 Share Posted November 14, 2009 All 'echo' and 'print', also anything outside the php code tags are sent directly to the output stream... Link to comment https://forums.phpfreaks.com/topic/181495-solved-warning-cannot-modify-header-information-headers-already-sent-by/#findComment-957426 Share on other sites More sharing options...
rarebit Posted November 14, 2009 Share Posted November 14, 2009 p.s. The meta tag version works at the browser level and can be overidden, whereas 'header' works at the server level. Link to comment https://forums.phpfreaks.com/topic/181495-solved-warning-cannot-modify-header-information-headers-already-sent-by/#findComment-957429 Share on other sites More sharing options...
mga_ka_php Posted November 14, 2009 Share Posted November 14, 2009 are you sure the meta didn't work, i use it a lot without a problem. if the meta didn't work. move <?php // Connects to your Database mysql_connect("localhost", "0607197", "12345") or die(mysql_error()); mysql_select_db("db0607197") or die(mysql_error()); //Checks if there is a login cookie if(isset($_COOKIE['ID_my_site'])) //if there is, it logs you in and directes you to the members page { $username = $_COOKIE['ID_my_site']; $pass = $_COOKIE['Key_my_site']; $check = mysql_query("SELECT * FROM Users WHERE username = '$username'")or die(mysql_error()); while($info = mysql_fetch_array( $check )){ if ($pass != $info['password']){ }else{ header("Location: book1.php"); } } } //if the login form is submitted if (isset($_POST['submit'])) { // if form has been submitted // makes sure they filled it in if(!$_POST['username'] | !$_POST['pass'] | !$_POST['pass'] ) { die('<h2><b>You did not fill in a required field.</b><a href="login.php"><font color="#b9c059">Back</a></font></h2>'); } // checks it against the database if (!get_magic_quotes_gpc()) { $_POST['email'] = addslashes($_POST['email']); } $check = mysql_query("SELECT * FROM Users WHERE username = '".$_POST['username']."'")or die(mysql_error()); //Gives error if user dosen't exist $check2 = mysql_num_rows($check); if ($check2 == 0) { die('<h2>No Such Account. <a href=register.php><font color="#b9c059">Click Here to Register</a></font></h2>'); } while($info = mysql_fetch_array( $check )) { $_POST['pass'] = stripslashes($_POST['pass']); $info['password'] = stripslashes($info['password']); $_POST['pass'] = md5($_POST['pass']); //gives error if the password is wrong if ($_POST['pass'] != $info['password']) { die('<h2> Incorrect password, please <a href=login.php><font color="#b9c059">Try again.</a></font>'); } else { // if login is ok then we add a cookie $_POST['username'] = stripslashes($_POST['username']); $hour = time() + 3600; setcookie(ID_my_site, $_POST['username'], $hour); setcookie(Key_my_site, $_POST['pass'], $hour); //then redirect them to the members area header("Location: book1.php"); } } } // if they are not logged in ?> before <html> if your going to use header(), always put that before <html> Link to comment https://forums.phpfreaks.com/topic/181495-solved-warning-cannot-modify-header-information-headers-already-sent-by/#findComment-957432 Share on other sites More sharing options...
DanielHardy Posted November 14, 2009 Author Share Posted November 14, 2009 I used regiemon's method. Will remember to do this in future. The php code that included headers in my other page was indeed before the html. I also noticed that I was trying to redirect the page to itself in certain instances. So this was causing problems too. Thanks for all of your help. Link to comment https://forums.phpfreaks.com/topic/181495-solved-warning-cannot-modify-header-information-headers-already-sent-by/#findComment-957451 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.