Jump to content

Headers not sent.


3raser

Recommended Posts

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);
}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
}

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.