How to Redirect the User In PHP ? See Example

Redirecting users is a very common task in PHP. Whether you want to send a user to a login page, forward them after form submission, or move them to another section of the website, PHP provides simple and effective methods to handle redirection.

This guide explains how redirection works, best practices, common errors, and real-world use cases.


⭐ What Is Redirection in PHP?

Redirection means sending the user’s browser to a different URL automatically using PHP.
PHP achieves this using the header() function, which sends raw HTTP headers to the browser.

The most common redirect command is:

header("Location: newpage.php");
exit();

⭐ How to Redirect in PHP (Basic Redirect)

Example: Redirect to another page

<?php
header("Location: home.php");
exit();
?>

Important:
Always use exit() after redirecting so the script stops running.


⭐ Redirecting to an External URL

<?php
header("Location: https://google.com");
exit();
?>

This sends the user outside your website.


⭐ Redirect After Form Submission

Widely used in login systems, contact forms, and checkout pages.

<?php
if ($_POST['username'] == "admin") {
    header("Location: dashboard.php");
    exit();
} else {
    header("Location: login.php?error=1");
    exit();
}
?>

⭐ Redirecting with Delay (HTML-Based Redirect)

PHP cannot directly delay header() redirects.
But you can use HTML meta refresh:

<?php
echo "<meta http-equiv='refresh' content='3; url=thankyou.php'>";
echo "Redirecting in 3 seconds...";
?>

Useful for displaying messages before redirecting.


⭐ Redirect with Status Codes (301 & 302)

302 Temporary Redirect (Default)

Most redirects in PHP are temporary:

header("Location: page.php", true, 302);
exit();

301 Permanent Redirect (SEO)

Used for URL changes, old pages, duplicate content:

header("Location: https://example.com/new-page", true, 301);
exit();

⭐ Conditional Redirects

Redirect based on conditions:

<?php
if (!isset($_SESSION['user'])) {
    header("Location: login.php");
    exit();
}
?>

Used for protected pages or role-based access.


⭐ Redirecting With GET Parameters

header("Location: profile.php?id=20&status=active");
exit();

In profile.php:

echo $_GET['id'];
echo $_GET['status'];

⭐ Most Common Error: “Headers Already Sent”

You get this error when you output anything before calling header().

Example that causes error:

echo "hello";
header("Location: page.php");  // ❌ ERROR

Fix:

  • No HTML before PHP header
  • No spaces/empty lines outside PHP tags
  • Use output buffering (ob_start())

Correct:

<?php
ob_start();
header("Location: page.php");
exit();
?>

⭐ Redirect Using PHP + JavaScript (Fallback Method)

Useful if headers cannot be used:

echo "<script>window.location.href='home.php';</script>";

⭐ Real-World Use Cases

✔ Redirect user after login
✔ Redirect unauthorized users to login page
✔ Move user to a thank-you page after submitting a form
✔ Redirect old URLs to new ones (SEO)
✔ Redirect based on device (mobile/desktop)
✔ Redirect based on language or location


⭐ Best Practices for PHP Redirects

✔ Always use exit() after a redirect
✔ Avoid any output before header()
✔ Use 301 redirects for SEO changes
✔ Use conditions for protected pages
✔ Use meta refresh only when necessary
✔ Redirect early in the code (top of file)


Citations

🔗 View other articles about PHP:
http://savanka.com/category/learn/php/

🔗 External PHP Documentation:
https://www.php.net/manual/en/

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *