Jump to content

Warning: Cannot modify header information - headers already sent..


Recommended Posts

I can't seem to figure this out. Everything seems to work fine with WAMP until I upload it online.

 

Warning: Cannot modify header information - headers already sent by ..on line 96 which is:

header ('Location: index2.php');

 

<?php
//Start session
session_start();
include ('../c/connect.php');
$ip = $_SERVER['REMOTE_ADDR'];
/*
THIS PAGE ASKS 1 QUESTION PRIOR TO REDIRECTING TO THE MAIN LOGIN PAGE.
*/
?>
<center>
<br />
<b>YOUR IP IS: <?php echo"$ip"; ?></b>
<br />
<b>Login below to proceed...</b>
<br />
<br />
<?php
//Select and Retrieve and print question/answer query
$qselect = "SELECT * FROM question";

if ($qresult = mysql_query ($qselect)) {
while ($row = mysql_fetch_array ($qresult)) {
$question = $row['question'];
$answer = $row['answer'];
$answer = mysql_real_escape_string ($answer);	
}
}

//Select and Retrieve username
$uselect = "SELECT * FROM admin";

if ($uresult = mysql_query ($uselect)) {
while ($row = mysql_fetch_array ($uresult)) {
$username = $row['username'];
$username = mysql_real_escape_string ($username);
}
}

//Select and Retrieve password
$pselect = "SELECT * FROM admin";

if ($presult = mysql_query ($pselect)) {
while ($row = mysql_fetch_array ($presult)) {
$password = $row['password'];
$password = mysql_real_escape_string ($password);
}
}
//We need to select all the ips from the database and compare them to the current one
$countip = mysql_query ("SELECT COUNT(ip) AS currentip FROM ip WHERE ip = '$ip'");
$totalcountip = mysql_fetch_array($countip);
//Put array in a variable for total current ip count
$totalcurrentip = $totalcountip['currentip'];
//Get date and time
$timestamp = time();

//If there are 5 or more of the same ips that entered a wrong input we will add the ip in the ipbanned database
if ($totalcurrentip > 4) {
//See if the ip exist
$selectipbanned = "SELECT ip FROM ipbanned";
if ($selectipbannedresult = mysql_query ($selectipbanned)) {
	while ($row = mysql_fetch_array ($selectipbannedresult)) {
	$ipbanneddata = $row['ip'];
	}
} 
//If ip does not exist add it to database else redirect to error.php
if ($ipbanneddata != $ip) {
	mysql_query ("INSERT INTO ipbanned (ip) VALUES ('$ip')");
} else {
header ('Location: error.php');
}
}

//Select and Retrieve banned ips
$ipbannedselect = "SELECT ip FROM ipbanned";

if ($ipbannedresult = mysql_query ($ipbannedselect)) {
while ($row = mysql_fetch_array ($ipbannedresult)) {
$ipbanned = $row['ip'];
}
}

//Form submission
//If current ip does not equal banned ips we allow the form else we direct it to error.php
if ($ip != $ipbanned ) {
if (isset ($_POST['submit'])) {
if ( (!empty ($_POST['username'])) && (!empty ($_POST['password'])) ) {
	//Check username and password
	if ( ($_POST['username'] == "$username") && ($_POST['password'] == "$password") ) {
		//Check answer
		if (!empty ($_POST['answer']) ) {
			if ($_POST['answer'] == "$answer") {
			$_SESSION['username'] = "$username";
			//Redirect to index2.php
			header ('Location: index2.php');
			exit ();		
			}
			else {
			//Insert ip to database
			mysql_query ("INSERT INTO ip (ip,time) VALUES ('$ip','$timestamp')");
			echo '<font color="red">Your answer was incorrect.</font><br />';
			}
		}
		else {
		echo '<font color="red">You left something blank.</font><br />';
		}
	}
	else {
	//Insert ip to database
	mysql_query ("INSERT INTO ip (ip,time) VALUES ('$ip','$timestamp')");
	echo '<font color="red">Wrong username or password.</font><br />';
	}
}
else {
echo '<font color="red">You left something blank.</font><br />';
}
}//End submit if
} else { //If ip is banned we redirect to error.php
header ('Location: error.php');
}
?>
<br />
<form action="index.php" method="post">
Username <input type="text" name="username" size="20" maxlength="20" />
<br />
<br />
Password <input type="password" name="password" size="20" maxlength="20" />
<br />
<br />
<b>
<?php
echo "$question";
?>
</b>
<br />
<br />
Answer <input type="text" name="answer" size="20" maxlength="20" />
<br />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</center>

Really, this error is pretty self explanatory.

 

You have output before a header call.

 

Why it may work on WAMP and not production, WAMP may have a setting in .htaccess or apache to buffer the output, similar to ob_start before sending it to the browser, which would be like a bandaid on the issue.

 

I would suggest following the actual rules for header calls and re-do your code appropriately.

Unfortunately, a lot of the W/L-AMP packages and the php.ini recommend settings turn output buffering on in php.ini, which means you can end up developing code containing content/header problems that does not work on a live server. While you might be able to turn output buffering on, it is better to correct your code so that it will work on all servers regardless of the output buffering setting or your ability to turn the setting on (some hosts won't permit you to do so.)

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.