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.
Related
Using an output buffer requires the server to store the entire output of the PHP in RAM, so if I have a large page, I'll wind up using a fair amount of memory - and the server will also have to wait until the entire page is generated before sending it out, which could cause a small delay. that's right ?
I don't want to know the advantage of using ob_start();. My problem is redirecting and this error: Headers already sent.
So for solving that problem, I used of ob_start(); in the fist of my codes. something like this:
<?php ob_start(); ?>
<?php
// 500 lines of code is here
header(Location: www.example.com/test.php);
?>
<html>
// 1000 lines of code is here
</html>
<?php ob_end_flush(); ?>
Now my problem has been solved, just I want to know everything is ok ? my codes are optimized ? If my requests rise, my site does not delay ?
thanks
The proper solution to the "Headers already sent" problem is described in a previous thread.
Basically, the correct cause of action is to move all of the processing code above any output to the browser. Then simply echo out the results, as needed, in between the HTML code.
Not only will you notice an improvement in the resource usage of the page, but you'll also notice that it will become a whole lot easier to actually read and write the code.
If the output branches are complex enough, which means anything above a very basic script (simple guestbook, etc), a template engine might be well worth the time and effort to look at.
Output buffering is frequently used and I wouldn't worry about this. For example, this SO webpage takes up ~ 64 KiB, meaning 16384 of these pages fit in 1 GiB ram simultaneously.
Probably offtopic, but if you're going to send a Location header, do you even need to execute all the other code? You could just send the header and exit() immediately.
Say you have a big PHP web application doing echoing, output buffering, etc, all over the place. The end result is a finished HTML/XML document to the browser.
Now say you find yourself in a small function in a random place in a big PHP web application. Is there a simple way to output something, for example an HTML comment, in a way so that it is sent as the last thing to the browser?
Something like a hook for "when everything is finished send this piece of info as well to the browser"?
The end goal is basically to output some warnings or debug info in an easy way, without messing up the output.
There's register_shutdown_function(), which would essentially be the very last thing executed by PHP before terminating your script.
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.
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.
My site has a php page that prints out XML, for some reason though it's being truncated to 8KB in size, I've never encountered this before and all the other pages on the site remain un-truncated.
Where should I start looking for the problem and what could cause it to stop like this?
The site uses the Zend framework and the page in question uses the soap server.
Put this before the section where the output cuts off:
error_reporting(E_ALL);
ini_set('display_errors', 1);
Chances are there is just an error that is not being output. Also make sure your code does not contain the error suppression operator (# symobl) as this is a common cause of hard to detect errors: http://www.php.net/manual/en/language.operators.errorcontrol.php
If my first suggestion fixed your issue then I would suggest that you set up and test error handling correctly so that you will receive all errors in the future as this will save you a lot of time.
I'd expect an error if it was memory but have you tried increasing the memory limit in php.ini?
It could be some memory limit, it could be some arbitrary part of the script that is running at that part of the XML creation.
Check what errors you are getting, see if any errors are suppressed. And if all else fails, post some example code that is running at that 8KB mark.