ob_start is non-complaint!!

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
shotos
Forum Newbie
Posts: 10
Joined: Thu Jun 12, 2008 5:54 pm

ob_start is non-complaint!!

Post by shotos »

Hi y'all
i just started learning PHP and i just had a warning;
"Cannot modify header information - headers already sent"

after searching for information on this warning, i learnt i got to use ob_start() immediately
after <?php .......... but someway somehow, ob_start() is non-complaint in my php script.

thanks in advance.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: ob_start is non-complaint!!

Post by Christopher »

Certainly "non-complaint" is not the term you want to use. The output buffering functions will capture output from the script, but it simply sounds like you can some output going to the browser before you attempt to set the header information. It could be a space or return character, or some misunderstanding of how the output buffering functions work. Post some code.
(#10850)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: ob_start is non-complaint!!

Post by Ollie Saunders »

Google for headers already sent this is one of the most common PHP errors.
shotos
Forum Newbie
Posts: 10
Joined: Thu Jun 12, 2008 5:54 pm

Re: ob_start is non-complaint!!

Post by shotos »

this is the code below but what i meant to say is
the ob_start() function and ob_flush() function are not active.

Code: Select all

<?php
ob_start();// enabling output bufferring
include("database.php");//including the database connection
//verifying if inputs were supplied
$username = $_POST['username'];
$password = $_POST['password'];
 
if($username == ''){ unset($username); echo "No username submitted<br>";}
if($password == ''){ unset($password); echo "No password entered<br>";}
 
if (isset($username) && isset($password))
{
$ret = confirmUser($username, $password);
if(!$ret){
echo"Wrong username or password<br>";
//header("location: login.html");}//redirecting page
}
else{
echo "Login Successful.<a href=login_success.php>Click here to
continue</a>";
//header("location: login_success.php");
}
} else
{
 header("location: login.html");
} 
 
function confirmUser($username, $password){
global $link;
//checking user inputs
$username = trim($username);
$password = trim($password);
$username = htmlspecialchars($username);
$password = htmlspecialchars($password);
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
 
//selecting table and confirming username and password
$query = "select * from users where username='$username' and password='$password'"; 
$result = mysql_query($query, $link);
$count = mysql_num_rows($result);//real validation with rows affected by query
 
if($count==1){
return true;}
 
else{
return false; }
}
ob_flush();//outputting the data in the buffer as heading
?>
<html>
<body>
</body>
</html>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: ob_start is non-complaint!!

Post by superdezign »

shotos wrote:this is the code below but what i meant to say is
the ob_start() function and ob_flush() function are not active.
And how do you know this...? By what indication do you make that assumption?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: ob_start is non-complaint!!

Post by pickle »

That error you posted has a second part to it - it tells you where output started.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
shotos
Forum Newbie
Posts: 10
Joined: Thu Jun 12, 2008 5:54 pm

Re: ob_start is non-complaint!!

Post by shotos »

superdezign wrote
And how do you know this...? By what indication do you make that assumption?
because when i type <?php it immediately turns dark brown and when i type ob_start() it
stays black which is unlike functions in php.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: ob_start is non-complaint!!

Post by RobertGonzalez »

Ok, I think you are looking at this wrong. If you are basing the compliance of PHP on what your editor does when using certain functions then you are asking for problems later on.

The warning you are getting is probably the most commonly post issue on our community. A brief search here would return hundreds, if not thousands, of posts telling you that you are outputting information before calling a function that has to set a response header. That is why you are getting the warning.

Using output buffering to "fix" this is not a fix. It is a cheap hack work around. The best thing for you to do is not output anything until after all response headers have been sent.
Post Reply