PHP Redirect: How-to Guide
Posted February 19, 2010 by Nick Vogt in Programming
If you've been developing in ASP 3.0 (classic ASP) you may be used to calling the response.redirect() function whenever you need to redirect a user to a different page. You could call this function anywhere in the page, which was convenient. Now that you're all grown up and using PHP, you may be a little confused about how to redirect in PHP. In PHP, you redirect using the header() function and it must be called before any HTML. Like so:
The function is straightforward. Simply change index.php with the location you want. The exit function is also needed as it terminates the rest of the script from running.
A note to the spaghetti programmers out there. This may require quite a change in your coding practices. Perhaps you've read some people recommending that you perform redirects with Javascript so that you can continue to redirect people from within the HTML. That is a bad habit and you should start separating your code from your HTML. Besides, not everyone has Javascript enabled.
Here's a simple use case showing a basic form input and conditional redirect:
Keep in mind that you can use PHP to generate a dynamic location to redirect to:
<?php
header('Location: index.php');
exit;
?>
<html>
<body>
Hello world!
</body>
</html>
header('Location: index.php');
exit;
?>
<html>
<body>
Hello world!
</body>
</html>
The function is straightforward. Simply change index.php with the location you want. The exit function is also needed as it terminates the rest of the script from running.
A note to the spaghetti programmers out there. This may require quite a change in your coding practices. Perhaps you've read some people recommending that you perform redirects with Javascript so that you can continue to redirect people from within the HTML. That is a bad habit and you should start separating your code from your HTML. Besides, not everyone has Javascript enabled.
Here's a simple use case showing a basic form input and conditional redirect:
<?php
if($_POST['submit_btn'] == 'Home')
{
header('Location: index.php');
exit;
}
if($_POST['submit_btn'] == 'Search')
{
header('Location: search.php');
exit;
}
?>
<html>
<body>
<form method="post">
<input type="submit" name="submit_btn" value="Home">
<input type="submit" name="submit_btn" value="Search">
</form>
</body>
</html>
if($_POST['submit_btn'] == 'Home')
{
header('Location: index.php');
exit;
}
if($_POST['submit_btn'] == 'Search')
{
header('Location: search.php');
exit;
}
?>
<html>
<body>
<form method="post">
<input type="submit" name="submit_btn" value="Home">
<input type="submit" name="submit_btn" value="Search">
</form>
</body>
</html>
Keep in mind that you can use PHP to generate a dynamic location to redirect to:
<?php
if($_POST['loc'] != '')
{
header('Location: article.php?id=' . $_POST['loc']);
exit;
}
?>
<html>
<body>
<form method="post">
<select name="loc">
<option value="1">Article 1</option>
<option value="2">Article 2</option>
<option value="3">Article 3</option>
</select>
<input type="submit" value="Go!">
</form>
</body>
</html>
if($_POST['loc'] != '')
{
header('Location: article.php?id=' . $_POST['loc']);
exit;
}
?>
<html>
<body>
<form method="post">
<select name="loc">
<option value="1">Article 1</option>
<option value="2">Article 2</option>
<option value="3">Article 3</option>
</select>
<input type="submit" value="Go!">
</form>
</body>
</html>
| Series | PHP Tutorials |
|---|---|
| Tags | php redirect |
