Get Current URL in PHP

I know for most people, this has been done a million times… But it’s the little things that sometimes help.

So this is how you would grab the current request URL and feed it into a local variable so you can play with it.

<?php
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
?>

It just says, if we have an “https” request, then concatenate https onto the front of the servername with the request URI. If we have an “http” request, do the same.

Super easy.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.