How to get the previous page (referrer) URL in PHP?

Use $_SERVER["HTTP_REFERER"]. It has the address of the page from which the visitor came to the current page. Note that the spelling is “REFERER” (one R is missing), because of an old mistake in the HTTP standard.

<?php
$ref = $_SERVER["HTTP_REFERER"] ?? "No referrer";

echo $ref;
?>

Output:

Tutorials Class - Output Window

The value can be missing or empty, so always use ?? like in the example. It is sent by the browser, so it is not reliable. Never use it alone for security checks, use a CSRF token instead (see PHP Security Tips).


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.