How to Make a Redirect in PHP

PHP page redirect

Yes, you would use the header function.

/* Redirect browser */

It is a good practice to call exit() right after it so that code below it does not get executed.

Also, from the documentation:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

This means you should not echo anything right before the header() function, as doing so will more than likely throw an error. Also, you will need to verify that this code gets run before any other output as well.

How to redirect to another page using PHP

That's the problem. I've outputted a bunch of information (including the HTML to build the login page itself). So how do I redirect the user from one page to the next?

This means your application design is pretty broken. You shouldn't be doing output while your business logic is running. Go an use a template engine (like Smarty) or quickfix it by using output buffering).

Another option (not a good one though!) would be outputting JavaScript to redirect:

<script type="text/javascript">location.href = 'newurl';</script>

PHP multiple redirects

It's fine except there is no need to have two location calls, and you don't need the else:

$url = "index.php";
if ((isset($_POST['listing_type'])) && (!empty($_POST['listing_type']))) {
// Switch statement

header("Location: $url");

Make dynamic redirection php based on parameters sent

Even though this question has an accepted answer, I am providing the following solution because:

  1. The question was tagged as PHP, not JavaScript
  2. The accepted answer is not a dynamic solution as was originally requested

then redirect to a dynamic url based on the parameters received.

Example URL: http://localhost/test.php?hello=world&foo=bar

Getting the query string parameters:

$query = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
parse_str($query, $result);

echo '<pre>';
echo '</pre>';


[hello] => world
[foo] => bar

To perform the redirect:

header('Location:' . http_build_query($result));

This would redirect to:

Is it possible to redirect using PHP?

You could do something like this


How to redirect a page using onclick event in php?

You can't use php code client-side. You need to use javascript.

<input type="button" value="Home" class="homebutton" id="btnHome" 
onClick="document.location.href='some/page'" />

However, you really shouldn't be using inline js (like onclick here). Study about this here:

Here's a clean way of doing this: Live demo (click).


<button id="myBtn">Redirect</button>


var btn = document.getElementById('myBtn');
btn.addEventListener('click', function() {
document.location.href = 'some/page';

If you need to write in the location with php:

  <button id="myBtn">Redirect</button>
var btn = document.getElementById('myBtn');
btn.addEventListener('click', function() {
document.location.href = '<?php echo $page; ?>';

Related Topics

Leave a reply