Page 1 of 1
Redirect without header()
Posted: Sun Sep 18, 2005 2:07 pm
by Skara
Ok, here's the deal.. If the files are uploaded successfully, I want to redirect to another page. Otherwise, I just want to display an error.
Code: Select all
if (upload()) redirect();
else print('error');
Problem is, at this point I've already sent the headers, so I can't do a header(Location:) redirect.
I was thinking about just including the page I want to see, but then the addy would be goofy, which would cause other problems.
Ideas?
Posted: Sun Sep 18, 2005 2:15 pm
by sweatje
1) rearrange you code so you know what you are doing before you send headers/output (preferred)
2) send a javascript script to perform the redirect instead
Posted: Sun Sep 18, 2005 3:16 pm
by s.dot
perform the upload action on an intermediate page that will redirect after the upload. this doesn't send headers, and will be seamless to the user.
or, use javascript.
Posted: Sun Sep 18, 2005 8:04 pm
by Skara
ick. Well, I think I can just do without. It's not a feature that
has to be there. Thanks.

Posted: Sun Sep 18, 2005 9:16 pm
by John Cartwright
I would probably use meta refresh because some user's may have javascript disabled.
Posted: Mon Sep 19, 2005 6:30 am
by Jenk
Some browsers block redirects that us JavaScript.
Re align your code so that the PHP operations happen before
any output is sent to the browser, including headers.
It is bad practice to do other wise
Code: Select all
<?php
/* do php up here */
?>
<html>
<!-- HTML down here -->
</html>
Posted: Mon Sep 19, 2005 6:42 am
by Grim...
I agree somewhat, fellow Londoner, but what if you wanted to display different content depending on the results of an IF statement? You're not suggesting you build two seperate pages, are you?
Posted: Mon Sep 19, 2005 6:55 am
by Jenk
Nope. I'm suggesting you process all PHP functions etc. before sending any output.
Example:
Code: Select all
<?php
$text = (isset($_GET['text'])) ? htmlentities($_GET['text']) : '[blank]';
?>
<html>
<body>
<h3>The value of $text is:</h3>
<?php echo $text; ?>
</body>
</html>
So if you have any functions that sanitise input, or define content, or redirect users dependant upon results, this should occur before any output is presented.
It will also save on bandwidth, as you will only be sending content to the user when it is necessary. Sending a load of HTML + content to the user when they are not going to be looking at it anyway is a waste
BTW, an alternative (yet dirty) solution for the Original Posters problem can be the use of PHP's object buffering:
Code: Select all
<?php
ob_start();
/* rest of page */
ob_end_flush();
?>
This will buffer all output until the command ob_end_flush() is called, thus you can get away with code flow similar to the below:
This will fail with a "Header already Sent" Error.
Code: Select all
<?php
echo 'Hello world!';
header('Location: http://www.google.co.uk');
exit();
?>
This will not:
Code: Select all
<?php
ob_start();
echo 'Hello world!';
header('Location: http://www.google.co.uk');
exit();
ob_end_flush();
?>
Posted: Mon Sep 19, 2005 7:02 am
by CoderGoblin
Have to agree with Jenk here. Normally for my pages I have a variable holding all the HTML which will be output (or a class if that's your thing). The php is run and then the variable is output. No mixing PHP with HTML which makes life easier (especially if web designer wants to change the page format) and the whole thing more readable. Wherever possible I separate processing from output.