nathanmaxsonadil Posted July 21, 2007 Share Posted July 21, 2007 I'm really new to PHP :-\ I was wondering what to do with this error Warning: Cannot modify header information - headers already sent by (output started at /home/nathan/public_html/header.php:10) in /home/nathan/public_html/sidebar.php on line 64 Warning: Cannot modify header information - headers already sent by (output started at /home/nathan/public_html/header.php:10) in /home/nathan/public_html/sidebar.php on line 65 Warning: Cannot modify header information - headers already sent by (output started at /home/nathan/public_html/header.php:10) in /home/nathan/public_html/sidebar.php on line 68 I was searching it on google but everyone said that you just have to delete the space on the lines however The Lines are: 64: setcookie(ID_my_site, $_POST['username'], $hour); 65: setcookie(Key_my_site, $_POST['pass'], $hour); 68: header("Location: members.php"); Quote Link to comment Share on other sites More sharing options...
phpknight Posted July 21, 2007 Share Posted July 21, 2007 You have to set the cookie before sending anything, even whitespace, to the browser. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 21, 2007 Share Posted July 21, 2007 Every line you posted has to be done before ANY output to the browser, so thats the problem. Could you post your code to the script that contains the lines with the errors? Quote Link to comment Share on other sites More sharing options...
nathanmaxsonadil Posted July 21, 2007 Author Share Posted July 21, 2007 This is the code I have <?php // Connects to your Database mysql_connect("localhost", "My Username", "My Password") or die(mysql_error()); mysql_select_db("My Database") 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=reg.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 ?> <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <h2>Login</h2> Username:<br> <input type="text" name="username" maxlength="40"><br> Password:<br> <input type="password" name="pass" maxlength="50"> <input type="submit" name="submit" value="Login"> </form> <?php } ?> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 21, 2007 Share Posted July 21, 2007 Is that file headers.php or sidebar.php? Post the code to the other file as well...I don't see any output started in that before it should be. Quote Link to comment Share on other sites More sharing options...
nathanmaxsonadil Posted July 21, 2007 Author Share Posted July 21, 2007 Header.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Swap Invites</title> <meta name="keywords" content="" /> <meta name="description" content="" /> <link href="default.css" rel="stylesheet" type="text/css" /> <link rel="shortcut icon" href="favicon.ico" > </head> <body> <div id="header"> <h1>Swap Invites</h1> </div> <div id="menu"> <ul> <li class="first"><a href="index.php">Home</a></li> <li><a href="about.php">About</a></li> <li><a href="blog.php">Blog</a></li> <li><a href="cont.php">Contact</a></li> </ul> </div> Sidebar.php <div id="columnB"> <?php // Connects to your Database mysql_connect("localhost", "nathan_nathan", "qazwsxedcrfv") or die(mysql_error()); mysql_select_db("nathan_swapinvites") 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=reg.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 ?> <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <h2>Login</h2> Username:<br> <input type="text" name="username" maxlength="40"><br> Password:<br> <input type="password" name="pass" maxlength="50"> <input type="submit" name="submit" value="Login"> </form> <?php } ?> Quote Link to comment Share on other sites More sharing options...
nathanmaxsonadil Posted July 21, 2007 Author Share Posted July 21, 2007 Can someone help me? :'( Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 21, 2007 Share Posted July 21, 2007 If your including header.php on the top of the sidebar.php script, then that is definitely the problem. Your header.php file is all output. If header.php isn't included in sidebar.php, then try removing the top line from sidebar.php: <div id="columnB"> I'm not sure if that would be considered output or not. 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.