Pokebert Posted January 12, 2013 Share Posted January 12, 2013 Yes, I know the sticky'd topic said how to fix it, but I'm new to PHP (I just began to learn) and didn't quite understand how to fix this script. I get these three errors: Warning: Cannot modify header information - headers already sent by (output started at /home/u253096179/public_html/login.php:1) in /home/u253096179/public_html/login.php on line 117 Warning: Cannot modify header information - headers already sent by (output started at /home/u253096179/public_html/login.php:1) in /home/u253096179/public_html/login.php on line 119 Warning: Cannot modify header information - headers already sent by (output started at /home/u253096179/public_html/login.php:1) in /home/u253096179/public_html/login.php on line 125 My code is: My code is: <?php // Connects to your Database mysql_connect("***********", "**********", "************") or die(mysql_error()); mysql_select_db("**************") 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: members.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']) { die('You did not fill in a required field.'); } // 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('That user does not exist in our database. <a href=register.php>Click Here to Register</a>'); } 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('Incorrect password, please try again.'); } 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: members.php"); } } } else { // if they are not logged in ?> <html> <head> <title> NintendoVille - Login </title> <meta name='viewport' content='width=320'> <style> body { padding: 0px; margin: 0px; } #topscreen { width: 320px; height: 218px; background-color:#FFFFFF; } #bottomscreen { width: 320px; height: 212px; background-color:#C8C8C8; } #marquee { width: 320px; height: 20px; background-color:#B0B0B0; border-radius: 25px; } #credits { width: 320px; height: 20px; background-color:#B0B0B0; border-radius: 25px; } </style> </head> <body> <div id='topscreen'> <img src="NintendoVilleLogoBeta.jpg.jpg" width=320px height=217px> </div> <div id='bottomscreen'> <center> <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <table border="0"> <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" align="right"> <input type="submit" name="submit" value="Login"> </td></tr> </table> </form> </center> <br> <div id="credits"> <center> Copyright 2012 - NintendoVille</center> </div> </div> <?php } ?> Quote Link to comment Share on other sites More sharing options...
Pokebert Posted January 12, 2013 Author Share Posted January 12, 2013 It'd be appreciated if someone could fix the code for me, or at least tell me how to fix it. Quote Link to comment Share on other sites More sharing options...
Pokebert Posted January 12, 2013 Author Share Posted January 12, 2013 (edited) Great. Now I just found out my other script is having header issues too. Warning: Cannot modify header information - headers already sent by (output started at /home/u253096179/public_html/members.php:1) in /home/u253096179/public_html/members.php on line 64 <?php // Connects to your Database mysql_connect("***", "***", "***") or die(mysql_error()); mysql_select_db("***") or die(mysql_error()); //checks cookies to make sure they are logged in if(isset($_COOKIE['ID_my_site'])) { $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 the cookie has the wrong password, they are taken to the login page if ($pass != $info['password']) { header("Location: login.php"); } //otherwise they are shown the admin area else { echo "Admin Area<p>"; echo "Your Content<p>"; echo "<a href=logout.php>Logout</a>"; } } } else //if the cookie does not exist, they are taken to the login screen { header("Location: login.php"); } ?> Edited January 12, 2013 by Pokebert Quote Link to comment Share on other sites More sharing options...
haku Posted January 12, 2013 Share Posted January 12, 2013 Headers are data describing the document that is about to be sent. For example, the encoding of the document, the document filename, the creation date etc. You can also send a header that forces the browser to redirect to a different page (as you are doing in your script). The thing is, all headers have to be sent before any output is sent to the browser, because they describe the document to follow. When you get these errors, it means the script has sent some data to the browser, and then after that data has been sent, the script has tried to set a header. Well it can't set a header, because the headers have already been sent to the browser, before the data that was sent to the browser. Now you are probably thinking you haven't sent any data to the browser, because you haven't used 'echo' or 'print' before the headers. Look at the error messages: headers already sent by (output started at /home/u253096179/public_html/login.php:1) headers already sent by (output started at /home/u253096179/public_html/members.php:1) The message is telling you that the headers have already been sent to the browser, and the location where this happened. In your case, the first error is one line 1 of login.php, and in the second case at line 1 of members.php. I'd bet a dollar to a donut that you have your opening php tag on the second line of your document. Either that, or you have a space before your opening php tag on the first line. Either way, because PHP has not been opened, this line is sent to the browser, and then your script is executed. There is one other possibility as well however, if neither of the above is true. You may be sending a BOM (a type of invisible character) automatically due to the settings in your editor. To test this, you will have to look through the documentation of your editor and see if you can turn this option off. Quote Link to comment Share on other sites More sharing options...
Pokebert Posted January 12, 2013 Author Share Posted January 12, 2013 Headers are data describing the document that is about to be sent. For example, the encoding of the document, the document filename, the creation date etc. You can also send a header that forces the browser to redirect to a different page (as you are doing in your script). The thing is, all headers have to be sent before any output is sent to the browser, because they describe the document to follow. When you get these errors, it means the script has sent some data to the browser, and then after that data has been sent, the script has tried to set a header. Well it can't set a header, because the headers have already been sent to the browser, before the data that was sent to the browser. Now you are probably thinking you haven't sent any data to the browser, because you haven't used 'echo' or 'print' before the headers. Look at the error messages: headers already sent by (output started at /home/u253096179/public_html/login.php:1) headers already sent by (output started at /home/u253096179/public_html/members.php:1) The message is telling you that the headers have already been sent to the browser, and the location where this happened. In your case, the first error is one line 1 of login.php, and in the second case at line 1 of members.php. I'd bet a dollar to a donut that you have your opening php tag on the second line of your document. Either that, or you have a space before your opening php tag on the first line. Either way, because PHP has not been opened, this line is sent to the browser, and then your script is executed. There is one other possibility as well however, if neither of the above is true. You may be sending a BOM (a type of invisible character) automatically due to the settings in your editor. To test this, you will have to look through the documentation of your editor and see if you can turn this option off. ... there was a space behind the headers. :l Quote Link to comment Share on other sites More sharing options...
Pokebert Posted January 12, 2013 Author Share Posted January 12, 2013 It works now. Thanks. (this has gotta be the wold's biggest fail in the history of PHP XP) Quote Link to comment Share on other sites More sharing options...
haku Posted January 12, 2013 Share Posted January 12, 2013 Only insofar as that probably isn't a PHP developer out there who hasn't learned this the hard way. 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.