Thursday, 15 February 2007

PHP Redirecting to a relative URL

This comes directly from the PHP Manual, but I thought I might keep it here as a reference.

Note: HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

 * Redirect to a different page in the current directory that was requested 
$host  = $_SERVER['HTTP_HOST'];
$uri  = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';  // change accordingly

header("Location: http://$host$uri/$extra");

No comments :