Redirect user to another page using HTTP headers or javascript

Here are two ways to redirect the user to another page.

First you can send headers with the location of the new page. Note that this only works if you haven't sent anything to the browser.

<?php
function HTML_RedirectTo($page) {
    
    // If you are using sessions and want to pass the session_id use this
	//header("Location: http://{$page}?PHPSESSID=" . session_id());
    
	header("Location: http://{$page}");

	die();
}
?>

If for some reason you had to output something and the above method is not an option, you can use javascript:

<?php
function HTML_JSRedirectTo($page) {
    ?>
    <script language="javascript">
	   window.location ='< ?php echo $page; ?>';
	</script>
    <?php
    die();
}
?>
Leave a comment
Name
Email (optional)
Your email will only be used to show your Gravatar and to send you replies to this article (if you opt to receive them). You won't get ANY other messages at all!
Comment
No HTML allowed
Notify me via email
 
 
Share

Valid HTML 4.01 Strict