Skip to content

Instantly share code, notes, and snippets.

@kythin
Created November 29, 2012 04:55
Show Gist options
  • Save kythin/4166886 to your computer and use it in GitHub Desktop.
Save kythin/4166886 to your computer and use it in GitHub Desktop.
PHP or JS Redirect with optional timeout
<?php
/*
* Handy redirect function.
* Takes one URL parameter that can be relative or absolute.
* Optionally also takes timeout in seconds.
* Returns null.
* If headers have been sent, and no timeout of 0 specified, will do a PHP redirect.
* Otherwise, it will do a javascript redirect.
*/
function redirect($page, $timeout = 0) {
//php redirect if possible
if (!headers_sent() && !$timeout) {
header("HTTP/1.1 301 Moved Permanently");
header ("Location: ".$page);
exit;
}
//otherwise a js redirect
if ($timeout) {
echo "<script type='text/javascript'>
<!--
function getgoing() {
top.location='$page';
}
setTimeout('getgoing()',".($timeout*1000).");
//-->
</script>";
} else {
echo "<script type='text/javascript'>
<!--
top.location = '$page'
//-->
</script>";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment