Another header problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Another header problem

Post by SidewinderX »

you all know the error

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/sidex/public_html/ThemeSystem/index.php:5) in /home/sidex/public_html/ThemeSystem/index.php on line 37
I read that the most common reason for this error was because there are white spaces before the <?php and after the ?> but i checked all my files and there arnt any, and i didnt find many other reasons for this. The script works fine on my local server, but when i upload it to my web server i get these errors. I'd guess its a difference in the php.ini file, but since my host wont change my .ini file i have to fix the php. so can someone please help me.

Code: Select all

<?php
session_start();
require("config.php");

echo "<br><br><br><center>";
echo "<strong>Admin Login</strong><br><br>";
echo "<form method='post' action='index.php?action=check'>
		<table>
			<tr>
				<td>Username:</td>
				<td><input type='text' name='uname'></td>
			</tr><tr>
				<td>Password:</td>
				<td><input type='password' name='pword'></td>
			</tr><tr>
				<td></td>
				<td><input type='submit' value='login'></td>
			</tr><tr>
				<td></td>
				<td></td>

			</tr>
		</table>
	</form>";
echo "<center>";

if($_GET['action'] == 'check')
	{
		$username = $_POST['uname'];
		$password = md5($_POST['pword']);
		$q = "SELECT * FROM administrators WHERE username='$username' AND password='$password'";
		$result = mysql_query($q) or die ('Something is wrong with query: ' . $q . '<br>'. mysql_error());
		$row = mysql_fetch_assoc($result);
	if(mysql_num_rows($result) > 0)
		{
$_SESSION['username'] = $username;
Header("Location: admin.php");
		}
	else 
		{
		echo "<b>Incorrect username or password.</b>";
		}
	}
else 
	{

	}
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

echo "<br><br><br><center>";
This is line 5?
echo -> output -> no further http headers.

you can turn on output buffering or change the script.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Another header problem

Post by Luke »

It's because you are outputting content and then trying to add a header:
SidewinderX wrote:you all know the error

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/sidex/public_html/ThemeSystem/index.php:5) in /home/sidex/public_html/ThemeSystem/index.php on line 37
I read that the most common reason for this error was because there are white spaces before the <?php and after the ?> but i checked all my files and there arnt any, and i didnt find many other reasons for this. The script works fine on my local server, but when i upload it to my web server i get these errors. I'd guess its a difference in the php.ini file, but since my host wont change my .ini file i have to fix the php. so can someone please help me.

Code: Select all

<?php
session_start();
require("config.php");

echo "<br><br><br><center>"; // Outputting content
echo "<strong>Admin Login</strong><br><br>";
echo "<form method='post' action='index.php?action=check'>
		<table>
			<tr>
				<td>Username:</td>
				<td><input type='text' name='uname'></td>
			</tr><tr>
				<td>Password:</td>
				<td><input type='password' name='pword'></td>
			</tr><tr>
				<td></td>
				<td><input type='submit' value='login'></td>
			</tr><tr>
				<td></td>
				<td></td>

			</tr>
		</table>
	</form>";
echo "<center>";

if($_GET['action'] == 'check')
	{
		$username = $_POST['uname'];
		$password = md5($_POST['pword']);
		$q = "SELECT * FROM administrators WHERE username='$username' AND password='$password'";
		$result = mysql_query($q) or die ('Something is wrong with query: ' . $q . '<br>'. mysql_error());
		$row = mysql_fetch_assoc($result);
	if(mysql_num_rows($result) > 0)
		{
$_SESSION['username'] = $username;
Header("Location: admin.php"); // Now trying to send header... can't do that because headers are already sent
		}
	else 
		{
		echo "<b>Incorrect username or password.</b>";
		}
	}
else 
	{

	}
?>
Look into output buffering or just store all of your content into a variable and output it after all logic ie:

Code: Select all

session_start();
$html  = "Some content";
$html .= "Some other content";
if($foo) header("Location: admin.php");
echo $html;
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

ob_start(); did the trick
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

output buffering = band-aid. :?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

yes, feyd is right... just turing output buffering on doesn't fix the problem, it just hides it from the user. I would recommend reading up on it on php.net (follow the link I posted) and thinking about not only how to make the problem go away, but fixing it.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code properly, don't hide bad code. It is not that hard to fix and it will get rid of the warning.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

And especially html+header/location doesn't make sense ;)
Post Reply