PHP $_SERVER is an array containing information such as headers, paths, and script locations. To get current url we can use $_SERVER['REQUEST_URI']
and $_SERVER['HTTP_HOST']
. Here are some examples.
Url without host
<?php $url = $_SERVER['REQUEST_URI']; print $url ?>
/php-get-current-url/
Env: PHP version 7.4.33 (Linux)
Full url with host and scheme
<?php $url = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . $_SERVER['REQUEST_URI']; print $url; ?>
https://infoheap.com/php-get-current-url/
Env: PHP version 7.4.33 (Linux)
Full url – double slash format
<?php $url = "//" . $_SERVER["HTTP_HOST"] . $_SERVER['REQUEST_URI']; print $url; ?>
//infoheap.com/php-get-current-url/
Env: PHP version 7.4.33 (Linux)