FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


header_already_sent!





AOP Web Development
Hello guys! i just like to know on how do you fix in the redirecting in php by using the Location() function.... because most of the time i used it will have an error of Warning: information header already sent.. something like that.. For me instead of using this i use javascript script.. the window.location.. but i just want to know on how to do it really in php by fixing that thing.. thanks!
Manofgames
as always, can you please paste your source code, as there isnt a huge amount we can do without it.

usually a headers already sent error is caused when you try to execute a special function like session_start() after sending content (ie text) to the users browser.
AOP Web Development
for example Sad Sad Sad Sad Sad Sad Sad

<?php

if(!session_is_registered("txtpassword") and !session_is_registered("txtpassword")){

header("Location: login.php");
exit();
}

?>

That is the code example.. and then it send an error of information header

Cannot modify header information - headers already sent by (output started at /home/hl33559/public_html/login.php:10) in /home/hl33559/public_html/loginuser.php on line 41

just don't mind the link.. just focus to the error received.
Manofgames
replace 'and' with &&, and paste the WHOLE script, or at least the code uptil that bit.
qscomputing
Most likely culprit (it is for me anyway) is that you have blank lines outside of your PHP tags in your include files. These blank lines are echoed to the browser before your code is executed, which means that PHP sends the headers before you get a chance to specify the ones you want.

So the simple answer is to ensure that all your PHP files end with "?>\n" where \n is the appropriate newline for your platform. Yes, the \n is optional to PHP, but Good Practice dictates a \n at the end of EVERY text file, as it is considered to be a line terminator, not a line seperator.

So - delete blank lines at the end (and beginning, and anywhere else you escape from PHP) of your include files and see if that helps.
sonam
And more...
Sometimes I got this error when I have simple blank (space) after <?php in first line. I don't know why but Brick wall

Sonam
Mgccl
BTW... sometimes it could be because of you are using UTF and the [url=unicode.org/faq/utf_bom.html]BOM[/url]
jabapyth
@Mgccl: you need the httb: at the beginning for phpBB to recognize it as a link
vinx_18
You could place an ob_start(); function in the beginning of the page. This means to flush all output buffer. Also, leave no spaces at the start in coding php.
<-------no space here----> this will also cause some errors if your php.ini settings is strict.
<?php
ob_start();



?>
AOP Web Development
qscomputing wrote:
Most likely culprit (it is for me anyway) is that you have blank lines outside of your PHP tags in your include files. These blank lines are echoed to the browser before your code is executed, which means that PHP sends the headers before you get a chance to specify the ones you want.

So the simple answer is to ensure that all your PHP files end with "?>\n" where \n is the appropriate newline for your platform. Yes, the \n is optional to PHP, but Good Practice dictates a \n at the end of EVERY text file, as it is considered to be a line terminator, not a line seperator.

So - delete blank lines at the end (and beginning, and anywhere else you escape from PHP) of your include files and see if that helps.


Can you show as an example what do you mean about that.
qscomputing
Not sure what you mean but here goes anyway.

When I'm editing files, I tend to add several newlines to the end, because I find it more comfortable to work with files like that, don't ask why. So my file ends up looking like this:
Code:

<?php
code code code code
?>








My main script will then look something like this:
Code:

<?php
include('some_file_with_blank_lines_at_end.inc.php');
...
header('something');
...

By the time the header() line is reached, the blank lines at the end of the include file have already been send to the browser, so PHP can't send any more headers (being HEADers, they must go at the top of the response). But if I remove all the blank lines, then nothing has been sent to the browser before the header() line is reached, and it works. HTH.
sambhav
Hey! This can also happen if you are using echo. Instead os echo use print.
Manofgames
how would that change anything?
its still ouputing to the browser, same as echo.
Fire Boar
An ob_start is your best bet. Provided you are not using ob_start elsewhere, just add that line at the beginning of your main script. Done. No problems with headers.

But you'll also need to end the buffering - otherwise nothing will come out of the other end! Here's what you should do.

Code:
<?php
ob_start();

// rest of your script

ob_end_flush();
?>
shamil
The rule is that redirection must be made before any output to the page. As qscomputing and some others mentioned i prefer to find those extra characters and remove them. qscomputing has clearly explained it.
AOP Web Development
if i have a code something like this

Code:


<html>
<head>
</head>
<body>
<form action="index.php" method="post">
<label>Username<input type="text" name="name"></label>
<br>
<input type="submit" name="action" value="   Submit   ">
</form>
<br>
<br>
<?php
ob_start();
if(trim($_POST[action])=="Submit"){
      if($_POST[name]=="12345"){
         header("Location: index1.php");         
      }   
   }
ob_end_flush();   
?>
</body>
</html>




why is the error goes the user cannot modify to header.. header already sent.
but when i put the php tag on the top before the html page such like this




Code:

<?php
ob_start();
if(trim($_POST[action])=="Submit"){
      if($_POST[name]=="12345"){
         header("Location: index1.php");         
      }   
   }
ob_end_flush();   
?>
<html>
<head>
</head>
<body>
<form action="index.php" method="post">
<label>Username<input type="text" name="name"></label>
<br>
<input type="submit" name="action" value="   Submit   ">
</form>
<br>
<br>

</body>
</html>




the redirection works fine...

Well actually i just really confused of what is the best for redirection on a page... As of now im using a javascript for redirection.. but im just concern if the user client browser is the javascript is disable.. so the redirection will not work... Wink Wink Wink Wink Wink Rolling Eyes Rolling Eyes Rolling Eyes Rolling Eyes
Gundamxxg
with the php redirect script at the top of the page there, I'm pretty sure you won't need the ob_start() and ob_end() in there. I had a similar problem on an auto login script that I was writing, Output buffering was defaulted to off on the server I was working from and all I had to do was move my script to before the HTML tag on the document. I think that having those two parts of the script will slow down processing of the script, but it may be too little to notice, figured I'd give my 2c worth.
shamil
AOP Web Development wrote:
if i have a code something like this

Code:


<html>
<head>
</head>
<body>
<form action="index.php" method="post">
<label>Username<input type="text" name="name"></label>
<br>
<input type="submit" name="action" value="   Submit   ">
</form>
<br>
<br>
<?php
ob_start();
if(trim($_POST[action])=="Submit"){
      if($_POST[name]=="12345"){
         header("Location: index1.php");         
      }   
   }
ob_end_flush();   
?>
</body>
</html>


why is the error goes the user cannot modify to header.. header already sent.
but when i put the php tag on the top before the html page such like this

Code:

<?php
ob_start();
if(trim($_POST[action])=="Submit"){
      if($_POST[name]=="12345"){
         header("Location: index1.php");         
      }   
   }
ob_end_flush();   
?>
<html>
<head>
</head>
<body>
<form action="index.php" method="post">
<label>Username<input type="text" name="name"></label>
<br>
<input type="submit" name="action" value="   Submit   ">
</form>
<br>
<br>

</body>
</html>



the redirection works fine...

Well actually i just really confused of what is the best for redirection on a page... As of now im using a javascript for redirection.. but im just concern if the user client browser is the javascript is disable.. so the redirection will not work... Wink Wink Wink Wink Wink Rolling Eyes Rolling Eyes Rolling Eyes Rolling Eyes


Because first one outputs
Code:


<html>
<head>
</head>
<body>
<form action="index.php" method="post">
<label>Username<input type="text" name="name"></label>
<br>
<input type="submit" name="action" value="   Submit   ">
</form>
<br>
<br>
before redirection occurs. As already said no output must be made before redirection.
In second one u use ob_start(); before any output which keeps output in buffer without sending it.
sonam
I am allways using two files for redirection and I haven't any problem. For example: login.php or login.html have simple form with action check.php. In check.php I am checking is it user valid or not. If is not redirect him on login page if is OK redirect him on index page. Nothing to echo, ob_flush, etc. Just pure redirections.

Sonam
Fire Boar
AOP Web Development wrote:
Code:


<html>
<head>
</head>
<body>
<form action="index.php" method="post">
<label>Username<input type="text" name="name"></label>
<br>
<input type="submit" name="action" value="   Submit   ">
</form>
<br>
<br>
<?php
ob_start();
if(trim($_POST[action])=="Submit"){
      if($_POST[name]=="12345"){
         header("Location: index1.php");         
      }   
   }
ob_end_flush();   
?>
</body>
</html>


For that instance, you need something like this:

Code:
<?php
ob_start();
?>
<html>
<head>
</head>
<body>
<form action="index.php" method="post">
<label>Username<input type="text" name="name"></label>
<br>
<input type="submit" name="action" value="   Submit   ">
</form>
<br>
<br>
<?php
if(trim($_POST[action])=="Submit"){
      if($_POST[name]=="12345"){
         header("Location: index1.php");         
      }   
   }
?>
</body>
</html>
<?php
ob_end_flush();
?>


Basically, ob_start MUST be at the beginning of the script to catch all the output. ob_end_flush will output everything it has caught already. You can put it anywhere in the script AFTER any redirections have happened. Normally, the best practise is to put it right at the end.
Gundamxxg
If this

Code:
<?php
ob_start();
?>
<html>
<head>
</head>
<body>
<form action="index.php" method="post">
<label>Username<input type="text" name="name"></label>
<br>
<input type="submit" name="action" value="   Submit   ">
</form>
<br>
<br>
<?php
if(trim($_POST[action])=="Submit"){
      if($_POST[name]=="12345"){
         header("Location: index1.php");         
      }   
   }
?>
</body>
</html>
<?php
ob_end_flush();
?>


is your index.php then all you need to do is this...
Code:

<?php
if(trim($_POST[action])=="Submit"){
      if($_POST[name]=="12345"){
         header("Location: index1.php");         
      }   
   }
?>
<html>
<head>
</head>
<body>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>Username<input type="text" name="name"></label>
<br>
<input type="submit" name="action" value="   Submit   ">
</form>
<br>
<br>

</body>
</html>


this way your code is a little bit less clunky and you wont get your errors, php is a top-down script so what ever it reads first, it does first. Especially since you are referring the page to itself you shouldn't need to have header issues since most of your scripts should be before the html itself.

Btw the
Code:
<? echo $_SERVER['PHP_SELF']; ?>
makes it a lot easier to port the file around, and its better to have that as your form action if you are referring the data back to itself to be processed.
Fire Boar
True, though what I was posting was a hypothetical solution that could be applied to any situation. By the way, about the $_SERVER['PHP_SELF'] - isn't it easier to do <form action="" method="post">? I'm pretty sure that takes the action as the current page, but I might be mistaken. Can someone clarify that?
qscomputing
The solution is extremely simple - if you want to send headers there must be no output before you send the headers. This means that there must be no blank lines outside of your <?php ?> tags. This ob stuff is overkill - it puts more load on the server for a problem that can be solved quickly and neatly with properly-formatted script files.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.