Page 1 of 1
ob_start is non-complaint!!
Posted: Thu Jun 12, 2008 6:05 pm
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.
Re: ob_start is non-complaint!!
Posted: Thu Jun 12, 2008 6:35 pm
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.
Re: ob_start is non-complaint!!
Posted: Thu Jun 12, 2008 9:45 pm
by Ollie Saunders
Google for headers already sent this is one of the most common PHP errors.
Re: ob_start is non-complaint!!
Posted: Fri Jun 13, 2008 5:26 am
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>
Re: ob_start is non-complaint!!
Posted: Fri Jun 13, 2008 8:05 am
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?
Re: ob_start is non-complaint!!
Posted: Fri Jun 13, 2008 9:50 am
by pickle
That error you posted has a second part to it - it tells you where output started.
Re: ob_start is non-complaint!!
Posted: Fri Jun 13, 2008 11:07 am
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.
Re: ob_start is non-complaint!!
Posted: Fri Jun 13, 2008 11:21 am
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.