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
PHP Forms Handling
PHP Send Mail
PHP header() Function
PHP mail() Function
Exercises & Assignments
Create a PHP Form with 2 fields Name and Hobbies
Interview Questions & Answers
What are the form method Attribute available for submitting?
How can we submit a form without a submit button?
What is the difference between GET and POST method in PHP?
How to redirect a page in PHP?
How to get the browser details of a visitor in PHP?
How to get the previous page (referrer) URL in PHP?
How to get the IP address of a visitor in PHP?