Redirect without header()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Redirect without header()

Post 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?
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

ick. Well, I think I can just do without. It's not a feature that has to be there. Thanks. ;)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I would probably use meta refresh because some user's may have javascript disabled.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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>
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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();
?>
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
Post Reply