Redirect to another page in PHP with Location and the right status code
To redirect in PHP, call header('Location: /destination.php', true, 302) before sending the body. End the script with exit. Use 302 for an ordinary temporary move, 303 to open a GET result after POST, and 307 to carry over the method and body. PHP Manual · header() RFC 9110 · Redirection status codes
Send a 302 with Location and exit
With PHP 8.x, save the first two scripts as move.php and destination.php. Run the command in the practice folder and open http://127.0.0.1:8000/move.php. The destination reports GET and no POST value. If necessary, change the port in both the command and the URL. Stop the server with Ctrl+C.
<?php
declare(strict_types=1);
header('Cache-Control: no-store');
header('Location: /destination.php', true, 302);
exit;
<?php
declare(strict_types=1);
header('Content-Type: text/plain; charset=UTF-8');
header('Cache-Control: no-store');
$note = $_POST['demo_note'] ?? '(none)';
if (!is_string($note) || strlen($note) > 120 || preg_match('//u', $note) !== 1) {
http_response_code(422);
exit('The sample value must be valid UTF-8 text of at most 120 bytes.');
}
echo 'Request method: ', $_SERVER['REQUEST_METHOD'], PHP_EOL;
echo 'POST value: ', $note, PHP_EOL;
php -S 127.0.0.1:8000
header() sets a header while the script continues. Use exit to prevent later processing or output. /destination.php starts at the site root. If you deploy under a subdirectory, update the destination paths and form action together.
Choose a status code for the next request
| Status | When to use it | Next request |
|---|---|---|
| 302 Found | General temporary move | Client may change POST to GET |
| 303 See Other | Show a result after POST | Retrieve with GET |
| 307 Temporary Redirect | Temporary move that preserves the request | Preserves method and body |
| 301 Moved Permanently | Permanent address change | Client may change POST to GET |
| 308 Permanent Redirect | Permanent move that preserves the request | Preserves method and body |
Use 301 or 308 for a permanent address change. A cached redirect can outlast a test, so use temporary codes for temporary flows. When you need to preserve the method and body, choose 307 or 308. Clients can change POST to GET with 302 or 301.
Compare 303 and 307 after POST
Save the third script as method-demo.php in the same folder. The existing destination.php reports the received method and sample value as plain text. Nothing is saved to a file or database.
<?php
declare(strict_types=1);
header('Cache-Control: no-store');
$method = $_SERVER['REQUEST_METHOD'] ?? '';
if ($method === 'POST') {
$mode = $_POST['mode'] ?? null;
$note = $_POST['demo_note'] ?? null;
$codes = ['see-other' => 303, 'keep-post' => 307];
if (!is_string($mode) || !isset($codes[$mode])
|| !is_string($note) || strlen($note) > 120 || preg_match('//u', $note) !== 1) {
http_response_code(422);
exit('Check the sample input.');
}
header('Location: /destination.php', true, $codes[$mode]);
exit;
}
if ($method !== 'GET') {
http_response_code(405);
header('Allow: GET, POST');
exit;
}
header('Content-Type: text/html; charset=UTF-8');
?>
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Compare 303 and 307</title></head>
<body>
<form method="post" action="/method-demo.php">
<p><label>Sample value <input name="demo_note" value="sample"></label></p>
<button name="mode" value="see-other">Follow a 303</button>
<button name="mode" value="keep-post">Follow a 307</button>
</form>
</body>
</html>
- Open /method-demo.php, leave the value as sample, and choose 303.
- The destination reports GET and no POST value.
- Open the form again, keep sample, and choose 307. The destination reports POST and sample.
A 303 implements Post/Redirect/Get by opening a GET page after POST. Refreshing that page avoids resubmission, but preventing all duplicate writes and checking authorization are separate concerns. A 307 resends the POST body. Confirm the destination can handle the request and may receive the data.
Select redirect targets from a fixed list
Save the fourth script as route.php. /route.php?to=form opens the form, while /route.php?to=result opens the result. An omitted key defaults to the result. Unknown keys and arrays receive 400. The server selects a destination from its own mapping without accepting a URL from the requester. OWASP · Redirect Validation
<?php
declare(strict_types=1);
header('Cache-Control: no-store');
$routes = [
'result' => '/destination.php',
'form' => '/method-demo.php',
];
$key = $_GET['to'] ?? 'result';
if (!is_string($key) || !isset($routes[$key])) {
http_response_code(400);
exit('This destination is not allowed.');
}
header('Location: ' . $routes[$key], true, 302);
exit;
Even allowlisted pages must enforce the user’s actual permissions. API clients may handle redirects differently depending on their settings. Inspect both the status and Location of the initial response.
Inspect the redirect response and path
- For headers already sent, check for a BOM, whitespace before PHP, or output from included files.
- Inspect the first status and Location in the Network panel. The final 200 response is a separate request.
- Check root and subdirectory paths and whether the destination exists. Two redirects that point back to each other form a loop.
- If a changed 301 or 308 still leads to an old path, inspect browser and intermediary caching.
sleep(5) delays the PHP request for five seconds without showing an explanation page. If users need time to read a notice, consider letting them follow a regular link. PHP Manual · sleep()




