PHP how to get current url

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 5.5.9 (Linux)

Full url with host and scheme

<?php
$url = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . $_SERVER['REQUEST_URI'];
print $url;
?>
http://infoheap.com/php-get-current-url/
Env: PHP version 5.5.9 (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 5.5.9 (Linux)
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments