PDA

View Full Version : Redirecting using header() calls


Halide
08-18-2002, 12:32 PM
Since I had some trouble initially, here's a brief overview.

To redirect to another page, you can use the header() call as seen below. However, it must be before any HTML output. You could use output buffering to prevent any mishaps.

header("Location: http://full-url-goes-here/path/file.php?action=stuff");

Then you most likely should put exit(); after this, since anything after this will not be viewed anyway (I think).

For obtaining the URL, you can use the following:

$url = "http://" . $HTTP_HOST . $PHP_SELF;
$url_path = dirname($url);

[UPDATE] Sometimes if setting cookies before this, you will have trouble when clients read the cookie header before/after the Location: part... I'm no expert on it... but I fixed my problem by using a Refresh header instead with a delay...

localhoster
11-18-2002, 06:56 AM
?><script language="JavaScript">window.document.URL = "index.php";</script><?

The following code i believe is easier for redirecting at any time during code execution :)

iDxMan
11-18-2002, 09:14 AM
Then you rely on javascript which wont be 100%. I know I turn JS off from time to time.

Grizzly
11-20-2002, 12:30 PM
You should always use server-side redirects when possible. Relying on JS or META tags to do it will not always work.

As a basic rule of web development, do NOT rely on the client if you don't have to.

And if you absolutely need to make header() calls after HTML is generated, you can always use the output buffering library built into PHP. A simple call to ob_start() at the top of your PHP template is all you really need to do.

There's simply no excuse for using JS/Meta redirects IMO. It's lazy, sloppy, and relies too heavily on the client's browser capabilties.

ThermoDust
04-26-2003, 11:09 AM
Is there any way to delay the redirect? Isn't there a pause command or sleep.....

hypertm
06-12-2003, 04:25 PM
Originally posted by ThermoDust
Is there any way to delay the redirect? Isn't there a pause command or sleep.....


sleep ();

will dely execution in seconds

usleep ();

will delay in microseconds

(usleep does not work on window systems)

gufmn
06-12-2003, 05:23 PM
If using header():

header( 'refresh: 3; url=whatever.htm' );

just replace 3 w/ however many seconds you want it to wait.