3raser Posted January 29, 2012 Share Posted January 29, 2012 My error: Warning: Cannot modify header information - headers already sent by (output started at /www/zxq.net/w/e/b/webaskius/htdocs/index.php:7) in /www/zxq.net/w/e/b/webaskius/htdocs/functions.php on line 6 Now, I can't quite figure out why it's doing this. The section of code that is causing this problem is above any HTML tags, at the very beginning of the file. <?php include('functions.php'); ?> <link href="style.css" rel="stylesheet" type="TEXT/CSS"> <img src="logo.png" border="0"> <div id="maincontent"> As you can see, the functions that cause this error are at the VERY beginning of my file. This is where they are called within index.php: if(mysql_num_rows($query_verify_login) > 0) { create_cookie(10000, 'user', $username); redirect('index.php'); } else { echo 'Woops! You\'ve entered in the wrong username and password combination!'; } And finally, my functions: <?php //our cookie creator function function create_cookie($time, $name, $data) { setcookie($name, $data, time()+$time); } function redirect($url) { header('Location: '. $url .''); } function get_replies($id) { //set query to find posts that match our id $query_get_replies = mysql_query("SELECT * FROM replies WHERE to = {$id}"); return mysql_num_rows($query_get_replies); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/255993-headers-not-sent/ Share on other sites More sharing options...
AyKay47 Posted January 29, 2012 Share Posted January 29, 2012 http://www.phpfreaks.com/forums/index.php?topic=37442.0 Quote Link to comment https://forums.phpfreaks.com/topic/255993-headers-not-sent/#findComment-1312262 Share on other sites More sharing options...
3raser Posted January 29, 2012 Author Share Posted January 29, 2012 http://www.phpfreaks.com/forums/index.php?topic=37442.0 I've already read that... The PHP process in question is already in the top of the file where it's processed. Quote Link to comment https://forums.phpfreaks.com/topic/255993-headers-not-sent/#findComment-1312264 Share on other sites More sharing options...
Drummin Posted January 29, 2012 Share Posted January 29, 2012 Is there any space or line returns before <?php at the top of the page? Quote Link to comment https://forums.phpfreaks.com/topic/255993-headers-not-sent/#findComment-1312265 Share on other sites More sharing options...
AyKay47 Posted January 29, 2012 Share Posted January 29, 2012 setcookie() sends an HTTP header. Quote Link to comment https://forums.phpfreaks.com/topic/255993-headers-not-sent/#findComment-1312267 Share on other sites More sharing options...
PFMaBiSmAd Posted January 29, 2012 Share Posted January 29, 2012 The error message tells you where the output is occurring at that you must find and eliminate - output started at /www/zxq.net/w/e/b/webaskius/htdocs/index.php:7 (line 7) Line 7 and probably lines 1-6 of your index.php contains output that is going to the browser. You cannot send any characters to the browser before sending a header (cookie/redirect,...) If you want us to help identify what the output is, post lines 1-10 of index.php. Quote Link to comment https://forums.phpfreaks.com/topic/255993-headers-not-sent/#findComment-1312271 Share on other sites More sharing options...
3raser Posted January 29, 2012 Author Share Posted January 29, 2012 I checked and there is no whitespace before my first <?php tags where I include the functions Here you go, my whole index.php: <?php include('functions.php'); ?> <link href="style.css" rel="stylesheet" type="TEXT/CSS"> <img src="logo.png" border="0"> <div id="maincontent"> <?php //connect to the database mysql_connect('localhost', '', ''); mysql_select_db(''); //check if logged in with a cookie if(isset($_COOKIE['user'])) { //if not set, show them the textarea to post in if(!$_POST['submit_new_question']) { ?> <form action="index.php" method="POST"> <textarea name="question" maxlength="450"></textarea><br/> <input type="submit" value="WebASK!"> </form> <hr> <?php } else { //secure the data for database input, strip any possible html tags, and make nl2br turn to break tags $question = mysql_real_escape_string(strip_tags(nl2br($_POST['question']))); if(strlen($question) <= 40) { echo 'Your question needs to be at least fourty characters! Please go back in your browser via your browser back button.'; } else { //sumbit to database mysql_query("INSERT INTO questions VALUES (null, '{$question}', '{$username}', '". date('M-D-Y') ."', '". $_SERVER['REMOTE_ADDR'] ."')"); echo 'Thanks. Your question has been posted!'; } ?> <hr> <?php } } else { if(isset($_GET['login']) || isset($_POST['login'])) { //if they have already clicked the login button, let's process it if(isset($_POST['login'])) { $username = mysql_real_escape_string($_POST['username']); $password = md5($_POST['password']); //query to check if username/password combination exists $query_verify_login = mysql_query("SELECT * FROM accounts WHERE username = '{$username}' AND password = '{$password}' LIMIT 1"); if(mysql_num_rows($query_verify_login) > 0) { create_cookie(10000, 'user', $username); redirect('index.php'); } else { echo 'Woops! You\'ve entered in the wrong username and password combination!'; } } else { ?> <table> <form action="index.php" method="POST"> <input type="hidden" name="login" value="1"> <tr><td>Username</td><td><input type="text" name="username" maxlength="25"></td></tr> <tr><td>Password</td><td><input type="password" name="password" maxlength="32"></td></tr> <tr><td><input type="submit" value="Login"></td></tr> </form> </table> <?php } } elseif(isset($_GET['register']) || isset($_POST['register'])) { if(isset($_POST['register'])) { $username = mysql_real_escape_string($_POST['username']); $password = md5($_POST['password']); //query to check if username exists $query_verify_no_duplicates = mysql_query("SELECT * FROM accounts WHERE username = '{$username}' LIMIT 1"); if(mysql_num_rows($query_verify_no_duplicates) > 0) { echo 'Sorry! An account already exists with that username.'; } else { if(strlen($password) < 4) { echo 'You\'re password needs to be at least five characters.'; } else { //create account in the database mysql_query("INSERT INTO accounts VALUES (null, '{$username}', '{$password}', '". date('M-D-Y') ."', '". $_SERVER['REMOTE_ADDR'] ."', 0, 0)"); echo 'Congratulations! You\'ve successfully registered!'; } } } else { ?> <table> <form action="index.php" method="POST"> <input type="hidden" name="register" value="1"> <tr><td>Username</td><td><input type="text" name="username" maxlength="25"></td></tr> <tr><td>Password</td><td><input type="password" name="password" maxlength="32"></td></tr> <tr><td><input type="submit" value="Register"></td></tr> </form> </table> <?php } } else { } ?> <br/><br/><a href="index.php?login=true">Login</a> or <a href="index.php?register=true">Register</a> to post a question!<hr> <?php } //a query to run while extracing questions $query_extract_questions = mysql_query("SELECT * FROM questions ORDER BY id DESC"); //extract our questions/post data while($row = mysql_fetch_assoc($query_extract_questions)) { ?> <table> <tr><td>Posted by <? echo $row['username']; ?></td><td>Posted on <? echo $row['date']; ?></td><td><? get_replies($row['id']); ?></td></tr> <tr><td><div id="question"><? echo $row['question']; ?></div></td></tr> </table> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/255993-headers-not-sent/#findComment-1312339 Share on other sites More sharing options...
Pikachu2000 Posted January 29, 2012 Share Posted January 29, 2012 You don't have an exit() after your header() redirect, so it's possible, maybe even likely, the error is generated by code that follows it. Quote Link to comment https://forums.phpfreaks.com/topic/255993-headers-not-sent/#findComment-1312342 Share on other sites More sharing options...
3raser Posted January 29, 2012 Author Share Posted January 29, 2012 You don't have an exit() after your header() redirect, so it's possible, maybe even likely, the error is generated by code that follows it. I attempted to add exit() right after header(), but still the same error: Warning: Cannot modify header information - headers already sent by (output started at /www/zxq.net/w/e/b/webaskius/htdocs/index.php:5) in /www/zxq.net/w/e/b/webaskius/htdocs/functions.php on line 6 Warning: Cannot modify header information - headers already sent by (output started at /www/zxq.net/w/e/b/webaskius/htdocs/index.php:5) in /www/zxq.net/w/e/b/webaskius/htdocs/functions.php on line 11 Quote Link to comment https://forums.phpfreaks.com/topic/255993-headers-not-sent/#findComment-1312343 Share on other sites More sharing options...
Pikachu2000 Posted January 29, 2012 Share Posted January 29, 2012 Well, clearly, the error is referring to this output. <link href="style.css" rel="stylesheet" type="TEXT/CSS"> <img src="logo.png" border="0"> <div id="maincontent"> Nothing should be sent to the browser until a point is reached in your code where it has been determined that there will not be a need to send any headers. Quote Link to comment https://forums.phpfreaks.com/topic/255993-headers-not-sent/#findComment-1312353 Share on other sites More sharing options...
PFMaBiSmAd Posted January 29, 2012 Share Posted January 29, 2012 Everything on lines 2,3,4, up to the second <?php tag, on line 5, in your index.php file is OUTPUT that is being sent to the browser. Your page is then calling functions in your functions.php file that send headers (setcokie, header). You are trying to copy/paste together a web page. You need to instead put all the php logic first to determine if you are even going to stay on the page and if you are, to build the content that makes up your page in php variables. Then simply echo the php variables where you want each different section of output. Quote Link to comment https://forums.phpfreaks.com/topic/255993-headers-not-sent/#findComment-1312358 Share on other sites More sharing options...
3raser Posted January 30, 2012 Author Share Posted January 30, 2012 Thanks guys. I've managed to move around some stuff and I now only echo out through variables. All PHP is at the top of the page (most, just to make the header errors go away). Quote Link to comment https://forums.phpfreaks.com/topic/255993-headers-not-sent/#findComment-1312452 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.