How to determine the referer in PHP?

Spread the love

Sometimes, we want to determine the referer in PHP.

In this article, we’ll look at how to determine the referer in PHP.

How to determine the referer in PHP?

To determine the referer in PHP, we use $_SERVER['HTTP_REFERER'].

For instance, we write

if (!empty($_SERVER["HTTP_REFERER"])) {
    header("Location: " . $_SERVER["HTTP_REFERER"]);
} else {
    header("Location: index.php");
}

to check if the $_SERVER["HTTP_REFERER"] isn’t set with empty.

If it’s set then, we call header to return $_SERVER["HTTP_REFERER"] as the value of the Location response header.

Conclusion

To determine the referer in PHP, we use $_SERVER['HTTP_REFERER'].

Leave a Reply

Your email address will not be published.