Backend Development 2 min read

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 "&lt;!--<SCRIPT LANGUAGE=\"javascript\">";
echo "location.href='$url'";
echo "&lt;/SCRIPT&gt;--&gt;";
?&gt;

4. HTML META refresh redirect

&lt;HTML&gt;
&lt;HEAD&gt;
    &lt;META HTTP-EQUIV="REFRESH" CONTENT="10" URL=www.php.cn/&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;

5. HTTP header redirect with explicit status code

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

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.

JavaScriptWeb DevelopmentheaderMeta 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

login 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.