Various Methods for Implementing Page Redirection in PHP

This article explains five different techniques for performing page redirects in PHP, including a one‑line header redirect, conditional redirects based on cookies, JavaScript‑based redirects, META refresh tags, and explicit HTTP header redirects with status codes.

php Courses
php Courses
php Courses
Various Methods for Implementing Page Redirection in PHP

In PHP development, implementing page redirection can be achieved through several approaches. Below are five common methods, each illustrated with sample code.

1. One‑line PHP redirect

<?php
$url = $_GET['url'];
Header("Location:$url");
?>

2. Conditional redirect using an if statement

if($_COOKIE["u_type"]) {
    header('location:register.php');
} else {
    setcookie('u_type','1','86400*360'); // set cookie long‑term
    header('location:zc.html');
}

3. JavaScript‑based redirect generated by PHP

<?php
$url = czbin.php;
echo "<!--<SCRIPT LANGUAGE=\"javascript\">";
echo "location.href='$url'";
echo "</SCRIPT>-->";
?>

4. HTML META refresh redirect

<HTML>
<HEAD>
    <META HTTP-EQUIV="REFRESH" CONTENT="10" URL=www.php.cn/>
</HEAD>
<BODY>
</BODY>
</HTML>

5. HTTP header redirect with explicit status code

<?php
$url = czbin.php;
Header("HTTP/1.1 303 See Other");
Header("Location: $url");
exit;
?>

These snippets demonstrate how to control client navigation using PHP’s header function, conditional logic, embedded JavaScript, META tags, or explicit HTTP status codes, allowing developers to choose the most suitable method for their application.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backendheadermeta refreshpage redirection
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.