Getting The Current URI In PHP

The $_SERVER superglobal array contains lots of information about the current page location. You can print this off in full using the following line of code.

echo '<pre>'.print_r($_SERVER, true).'</pre>';

Although this array doesn't have the full URI we can piece together the current URI using bits of the $_SERVER array. The following function does this and returns a full URI.

function currentUri(){
 $uri = 'http';
  if($_SERVER['HTTPS'] == 'on'){
   $uri .= 's';
 $uri .= '://';
 if($_SERVER['SERVER_PORT'] != '80'){
 return $uri;

You can use this function like this:

echo currentUri();


Add new comment

The content of this field is kept private and will not be shown publicly.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
7 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.