Cronthenoob Posted August 21, 2006 Share Posted August 21, 2006 What causes this error? And how can I fix it?Warning: Cannot modify header information - headers already sent by (output started at Site\index.php:4) in Site\login.php on line 27I only get it when I am trying to set a cookie. Link to comment Share on other sites More sharing options...
Akula_dude Posted August 21, 2006 Share Posted August 21, 2006 what's your code? Link to comment Share on other sites More sharing options...
Cronthenoob Posted August 21, 2006 Author Share Posted August 21, 2006 The Login Page :: <? if ($_GET['action'] == "login") { $login_password=$_POST['password']; $login_username=$_POST['username']; $query="select * from table where username='$login_username'"; $results=mysql_query($query); echo mysql_error(); $e=mysql_fetch_array($results); $veri_password=$e[password]; $activated=$e['valid_email']; $veri_username=$e['username']; if (mysql_affected_rows() < 1) { echo "<div class='error'>Username Does Not Exist!<br /></div>"; } if (mysql_affected_rows() >= 1) { if ($login_password != $veri_password) { echo "<div class='error'>Incorrect Password!<br /></div>"; } elseif ($login_password == $veri_password) { echo "Hello, ".$veri_username."! (<a href='index.php?login=logout'>log out</a>)"; $cookie_data= $e['username'].'-'.$e['password'].'-'.$e['e_mail'].'-'.$e['u_id']; setcookie("welcomeback", $cookie_data, time()+3600); $_GET['login'] = "ignoredefaultlogin"; } } } if ($_GET['login'] != "ignoredefaultlogin") { if (isset($_COOKIE['welcomeback'])){ $cookie_info = explode("-", $_COOKIE['welcomeback']); $username=$cookie_info[0]; $password=$cookie_info[1]; $email=$cookie_info[2]; $userid=$cookie_info[3]; echo "Hello, ".$username."! (<a href='index.php?login=logout'>log out</a>)"; $_GET['login'] = "ignoredefaultlogin"; } else { ?> <a href="index.php?login=register" class="links">Click here to register!</a> <br /> <form name="login" method="post" action="index.php?action=login" class="form"> <font class="formtext">Username: <br /> <input type="text" name="username" size="15"> <br /> Password: <br /> <input type="password" name="password" size="15"></font> <div style="height:5px;margin:0px;padding:0px;"></div> <input type="submit" class="standard_button" value="Login"> </form> <? } } The index page where the login page is called:: <table height="100%" width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><?PHP include("login.php");?></td> </tr> </table> Link to comment Share on other sites More sharing options...
ThePsion5 Posted August 21, 2006 Share Posted August 21, 2006 Header and cookie information have to be set before any output occurs. And see that extra line up at the top? That's output, lol.I only know this from making the same mistake many times. Also, I would advise that you use <?PHP instead of <?, as the latter can also signify XML code.Another thing is that you may not be able to set cookie information in an included file if there is output before that file is included...same deal with there being output to the browser before the header information is set. You'll probably have to deal with cookies in your primary page instead of the included one. Link to comment Share on other sites More sharing options...
Cronthenoob Posted August 21, 2006 Author Share Posted August 21, 2006 Header and cookie information have to be set before any output occurs. And see that extra line up at the top? That's output, lol.I only know this from making the same mistake many times. Also, I would advise that you use <?PHP instead of <?, as the latter can also signify XML code.On the actual page that space isnt there. And i went ahead and added the PHP Link to comment Share on other sites More sharing options...
Little Goat Posted August 21, 2006 Share Posted August 21, 2006 echo mysql_error();that is sending the output. you must have no output before the cookie statement, including echos, print()s and stuff in between the php segments. if possible, move the cookie statement to before all those, or take them out. Link to comment Share on other sites More sharing options...
Cronthenoob Posted August 21, 2006 Author Share Posted August 21, 2006 Thats strange, but i managed to get it to work by moving around a few things. Link to comment Share on other sites More sharing options...
reportingsjr Posted August 21, 2006 Share Posted August 21, 2006 You cant have any output, include spaces, html, or any of that stuff, before you set cookies. You can have any logic and such you want... Link to comment Share on other sites More sharing options...
ThePsion5 Posted August 21, 2006 Share Posted August 21, 2006 Thats strange, but i managed to get it to work by moving around a few things.Well, the reason that's the way things work is because how the content is presented is very dependant on header information, which includes letting the browser know what it's supposed to be displaying. If output began before the browser knew this information, it could do things like try and render a movie file as HTML (which has actually happened to me before), and do other things that are generally undesirable and maybe even dangerous to your computer's health, lol. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 21, 2006 Share Posted August 21, 2006 This was your problem: <table height="100%" width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><?PHP include("login.php");?></td> </tr> </table> The table tags (and all HTML headers) get sent before you include the login page. That is where the headers are being sent. If you want to keep that structure, you can use output buffering to only send the output once the program is finished: <?php ob_start(); // start buffering?>...<table height="100%" width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td><?PHP include("login.php");?></td> </tr> </table>...<?phpob_end_flush(); //send the output?> Link to comment Share on other sites More sharing options...
Cronthenoob Posted August 22, 2006 Author Share Posted August 22, 2006 Weeeee . . . .You know too much about PHP, I can never find this stuff with google. Thanks.And I got it working perfectly. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now