This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I am trying to implement a fairly complex 3rd party script that consists of several interconnected scripts. I've included the intro of the script in a simple page of mine. The script basically writes different things to a string based on logic and echoes the string at the end. Sometimes it echoes a form and when you click on submit, the script runs again and based on the new logic echoes different text that displays fine inside my page where it is included in place of the earlier form. (The 3rd party script included in my page also has some includes of its own.)
My problem is that rather than have it echo some of the things it echoes, notably error os success messages, on occasion I would like to have it redirect to another page on my site.
I've done this successfully with other pages of mine. I include a script in a page that writes some header type code. Based on certain parameters or actions by the user that recall the script, the include may redirect to itself or another page. The only thing I have to make sure is that there are no spaces or text written in the course of the include prior to the redirect.
However working with this third party script, although I think I've removed all the white space, it is not letting me redirect. The error message sites the code written in my page that includes the 3rd party script. Here is the msg:
Warning: Cannot modify header information - headers already sent by (output started at /blah blah/stepone.php:5).
Step one is my simple page that calls the 3rd party script.
Am I right that an include can redirect in response to a user action even if there is some text currently displayed? Should I just be checking the third party scripts for white space or is there some structural thing I may be doing wrong.
The 3rd party scripts are too large to put in here otherwise I would put them in.
Thanks.
What you need, probably is ob_start(). This buffers the output and enables you to redirect, even when output is already generated.
Also check out the manual: http://php.net/manual/en/function.ob-start.php
the error you have mentioned happens because of headers being already send to the browser. and when it tries to send the header again it throws an error. so for example.
when you try doing something like this.
echo 'Hello World';
header('Location:some/location.php');
it might throw you an error. hence it is always good to place the redirect header on top. but sometimes we may not want that. in such case you can turn on output buffering by using php's ob_start() according to PHP's definition
This function will turn output buffering on. While output buffering is
active no output is sent from the script (other than headers), instead
the output is stored in an internal buffer
so instead of example code above you can use something like.
ob_start();
echo 'Hello World';
header('Location:some/location.php');
ob_end_flush();
You have to:
use ob_start() at the beginning of your file.
use ob_end_flush() at the end of your file.
Related
This question already has answers here:
How to disable output buffering in PHP
(6 answers)
Closed 6 years ago.
I have programmed and set up a php web application on a shared Host (using apache server). Before the server being transferred, on each page, header and sidebar and ... was loaded and displayed and each part of the script was displayed immediately after being processed. For example if data was fetched from database and being displayed in a table, after displaying table header, each row was processed and displayed and then the next row and etc.
There has been a server transfer and apparently some configurations might have been changed and now each page is only displayed when the whole page is completely processed and is being displayed all at once.
I was wondering what the problem might be.
Thanks in advance
The issue here is Output Buffering. What happens with Output Buffering is, the output is not sent immediately, but gets sent only after the whole page is processed including delays and sleep.
You can disable output buffering to get the same experience. But beware, you will get the dreadly Headers already sent. errors sometimes.
See How to disable output buffering in PHP to disable output buffering per page or whole.
I am writing an application that submits html forms to a php script. At the end of the php script I have header("Location: finished.html");.
The goal is that once all of the data has been analyzed /submitted to sql etc.. the page would redirect.
My question is is this okay practice? It seems unreliable to simply set the header at the end of the script being that various errors could happen throughout the script.
I don't want the page to simply end up at the php script, and I would like to know if there are errors throughout. I am doing as much error checking as I can.
You can use header() without problems as long as you did not send any response to the client using echo/print and the likes or normal text/html, beware of spaces/newlines before <?php
PHP stops processing once it encounters an error.
PHP will continue processing if it encounters a warning/notice, but if the onscreen error reporting is enabled, the warning generated output will prevent header() from executing as stated by #Simba in the comment.
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
Getting this error:
Warning: session_start() [function.session-start]: Cannot send session
cache limiter - headers already sent (output started at
/home/apspec/public_html/home.php:105) in
/home/apspec/public_html/login.php on line 1
I've done some searching and don't see any spaces. My session start is at line 1. I think I had this error once before and something on the server was adding a space. Is there a way to check if that is the case? Any other ideas?
Dressing up jeoroen's comment:
Just because you might have written <?php session_start(); right at the start of a file, doesn't necessarily mean it's the beginning of the script! As shown by your error, you have output beginning in home.php at line 105. Perhaps you have some header stuff there, then you called include login.php; to begin session stuff.
While "nothing must come before it" is a good rule of thumb, the more accurate rule is "the script must not send output before it". Personally, I have session_start() called two layers of included files deep and even then it's somewhere around line 60 (making games means I have a LOT of init code!) but output does not begin until after all code has run - in fact, in my latest project the very last line of code to run is $template->output();, a function which takes all of the work done so far and dresses it up in all the HTML it needs.
That last point is actually quite important. Many people, especially experienced programmers, will emphasise the importance of separating your layers. At its simplest, use external CSS and JS files instead of inlining stuff in your HTML, to separate content from style from functionality. The same applies to PHP. Ideally you should avoid outputting HTML willy-nilly in your code, instead your code should do all the processing it needs and then output it at the end.
Smarty is a widely-used engine for this, but in that project I mentioned earlier $template->output() just creates a clean function scope, imports the prepared data, and calls require $file.".tpl.php"; so I can have some final post-processing. I figured that was easier than installing an entire third-party engine :p I... I have trust issues with code other people wrote. Years ago, I lost gigabytes of forum posts to a glitch in phpBB... -shivers-
Anyway, hope this rambling helps!
Is there a way to redirect using PHP without using header("Location: http://www.google.com")? I put that at the top, right after a PHP script (which has no output), but it doesn't work. I use the PHP to check something in the database, and it will redirect depending on the contents.
"Right after a PHP script"? Well, it's going to have to be in a PHP script to work.
If that's not it, please consider showing your previous code. Remember, don't post a question asking how to implement your solution, but rather the question itself...
Your code should always work as long as the header is called before any echo or print statements that send output to the browser. Another possibility is your webserver sending out additional output or headers that are causing the redirect to not work.
One way to test would be to telnet to your webserver and send GET /myscript.php. Then view the result and see if it is what you expect.
Per the PHP documentation:
Remember that header() must be called before any actual output is
sent, either by normal HTML tags, blank lines in a file, or from PHP.
It is a very common error to read code with include(), or require(),
functions, or another file access function, and have spaces or empty
lines that are output before header() is called. The same problem
exists when using a single PHP/HTML file.
Without seeing the actual code that the redirect resides in it will be difficult to assist. Perhaps if you could provide more details then someone may be able to suggest another technology to help but the header method is the only one that I've came across.
I want code in a php5 page be executed after all of the page (or maybe even after the first part of the page) is displayed.
I would prefer no javascript and no frames / iframes and no redirects. Just one page written in php, only emitting html.
I remember some special function in php5 to register a callback function that will be executed after the content is sent to the browser, but could not find, did I dream this?
What are the most common ways to do this? I am on Linux / Apache.
It is not necessary to write a complete example, if you just name the ways or describe the trick and I lookup the details for myself
Edit: Now I am asking another question about the execution of the shutdown function here:
How to make sure the user cannot interrupt execution of php code called by "register_shutdown_function"?
Are you looking for register_shutdown_function()?