OK, so i have written a website that has been hosted online with a free hosting company for some time, but now i have limited access to the internet, so i have started using XAMPP. The code that once was fine on the hosting site has now stopped working. I have got this message:

Warning: Cannot modify header information - headers already sent by (output started at F:\Applications\_XAMPP\xampp\htdocs\game\db.php:10) in F:\Applications\_XAMPP\xampp\htdocs\game\log.php on line 19

Here is my code

<?php 
require_once('db.php'); // contains the database connect and select code

	if(isset($_POST['Login']))
	{
		if($_POST['username']!='' && $_POST['password']!='')
		{
		//Use the input username and password and check against 'users' table
		$query = mysql_query(
			"SELECT user_ID 
			FROM user 
			WHERE user_name = '".mysql_real_escape_string($_POST['username'])."' AND password = '".mysql_real_escape_string(($_POST['password']))."'");

		if(mysql_num_rows($query) == 1)
			{
			$row = mysql_fetch_assoc($query);
			$_SESSION['user_id'] = $row['user_ID'];
			$_SESSION['logged_in'] = TRUE;
			header("location: game/enter.php");
			}
		else	{ $error = 'Wrong Username or password';	}
		}
	else	{ $error = 'Please fill out both your username and password to access your account'; }
	}
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<table width="780">
<?php //if(isset($_SESSION['user_id'])) {unset($_SESSION['user_id']);echo "logged out";}?>
<tr><td>
Username:	</td><td><input type="text" name="username" size="20" value="" /></td><td>
Password:	</td><td><input type="password" name="password" size="20" value="" /></td><td>
	<input type="submit" name="Login" value="Play" /></td></tr></table>
</form>
<?php if(isset($error)){ echo $error;}?>

If anyone could tell me why this is happening i would be very grateful

The problem is with the file db.php . When you are using header() function, you shouldn't output anything to the browser. ie., you shouldn't echo anything before header function. You shouldn't even have html tags. Check your db.php.

Cheers,
Nav

The problem is with the file db.php . When you are using header() function, you shouldn't output anything to the browser. ie., you shouldn't echo anything before header function. You shouldn't even have html tags. Check your db.php.

Cheers,
Nav

Thankyou, this is a very good explanation as this code is near the bottom of the page. I have fixed the problem using ob_start() and ob_flush(), that allows me to keep everything the same.

ob_start buffers all the output. But, if your page has a lot of data, make sure that you don't use ob_start since it acts on the server load.

ob_start buffers all the output. But, if your page has a lot of data, make sure that you don't use ob_start since it acts on the server load.

ok thanks again, what do you class as a lot of data, because i would guess the whole page has around 80 - 90 lines of code.

I have changed it so that the ob_start() and ob_flush() are only wrapped around the start of the html and the header(location:game/enter.php) and still get the same result. Will this have any effect on it??

80-90 lines of code.. hmm.. Then it doesn't make any difference.. When I said 'a lot of data', I meant, the contents on the screen, eg, images, thousands of lines of code, etc.. Actually, ob_start should be the first line of your code after <?php and ob_flush should be the last line.

This question has already been answered. Start a new discussion instead.