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.
ob_start is non-complaint!!
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: ob_start is non-complaint!!
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)
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Re: ob_start is non-complaint!!
Google for headers already sent this is one of the most common PHP errors.
Re: ob_start is non-complaint!!
this is the code below but what i meant to say is
the ob_start() function and ob_flush() function are not active.
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>- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: ob_start is non-complaint!!
And how do you know this...? By what indication do you make that assumption?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.
Re: ob_start is non-complaint!!
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.
Re: ob_start is non-complaint!!
superdezign wrote
stays black which is unlike functions in php.
because when i type <?php it immediately turns dark brown and when i type ob_start() itAnd how do you know this...? By what indication do you make that assumption?
stays black which is unlike functions in php.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: ob_start is non-complaint!!
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.
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.