Redirecting to another URL in PHP requires passing the Location header and exiting thereafter:
1 2 3 4 5 6 7 8 |
<?php function redirect($destination){ header("Location: ".$destination); exit; } ?> |
Though this method does work, I find it rather unclean. So, I prefer specifying the HTTP status code and also returning HTML/JavaScript for redirection. The HTML can be quite using when the page client is on a connection with very limited bandwidth.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php function redirect($destination){ header("HTTP/1.0 302 Found"); header("Location: ".$destination); $html=<<<EOD <html> <head> <META HTTP-EQUIV="refresh" content="1;URL=$destination"> <script type="text/javascript"> window.location = $destination; </script> </head> <body> <p>Redirection in progress...</p> <br> If the redirect fails just visit the following URL: <a href="$destination">$destination</a>. </br> </body> </html> EOD; echo $html; exit; } ?> |