While all the other answers work, they all have one big problem: the browser must decide what to do if they encounter the Location header. Typically, the browser stops processing the request and redirects to the URI specified in the Location header. But an attacker can simply ignore the Location header and continue to request it. In addition, there may be other things that cause the PHP interpreter to continue evaluating the script behind the Location heading, which is not how you planned.
Picture:
<?php if (!logged_id()) { header("Location:login.php"); } delete_everything(); ?>
What you want and expect is that users are not logged in, redirected to the login page, so that only registered users can delete everything. But if the script is executed after the Location header, everything is still deleted. So this is import, to ALWAYS put exit after the Location header, for example:
<?php if (!logged_id()) { header("Location:login.php"); exit;
So, to answer your question: redirect from a php page to another page (and not just php, you can redirect to any page this way), use this:
<?php header("Location:http://www.example.com/some_page.php"); exit;
A quick note: the HTTP standard says that you must specify absolute URLs in the Location header (http: // ... as in my example above), even if you just want to redirect another file in the same domain. But in practice, relative URLs (Location: some_page.php) work in all browsers, although they donβt comply with the standard.
source share