A Better PHP Redirect Method

PHP provides the header( ) function for redirecting users to another page, e.g.

[cc lang=”php”]header(“Location: http://www.google.com”);[/cc]

but, in case that doesn’t work, here’s a custom method that will attempt to redirect your users in 3 different ways:

[cc lang=”html”]
function redirect($target) {
if (!headers_sent()) {
header(“Location: $target”);
}

echo “n”;
echo “document.location = ‘” . $target . “‘;n”;
echo “

If you are not automatically redirected, please click here“;
exit;
}
[/cc]

then, you can redirect using this new function like this

[cc lang=”php”]redirect(“http://www.google.com”);[/cc]