How to get the full URL in PHP?

Spread the love

Sometimes, we want to get the full URL in PHP.

In this article, we’ll look at how to get the full URL in PHP.

How to get the full URL in PHP?

To get the full URL in PHP, we can use the $_SERVER['REQUEST_URI'] variable.

For instancce, we write

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

to create the full version of the current URL.

We use

(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http")

to add 'http' or 'https' depending on the value of isset($_SERVER['HTTPS']).

And then we add the hostname with $_SERVER[HTTP_HOST].

And we add the rest of the current URL with $_SERVER[REQUEST_URI].

Conclusion

To get the full URL in PHP, we can use the $_SERVER['REQUEST_URI'] variable.

Leave a Reply

Your email address will not be published.