# PHP Get Full Current Page URL with $_SERVER Variables

> Combine $_SERVER['HTTP_HOST'] and $_SERVER['REQUEST_URI'] to build the full URL of the current page in PHP, with differences between PHP_SELF and REQUEST_URI

**URL:** https://sentry.io/answers/how-do-i-get-the-full-url-in-php/

---

## The Problem

You want to get hold of the entire URL as it would appear in the browser's address bar. How do you do this in PHP? 

## The Solution

To get the full URL, we add the values of the `$_SERVER['HTTP_HOST']` and `$_SERVER['REQUEST_URI']` variables to the `https://` protocol:

```php
$requestUrl = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $requestUrl;
```

Example output:

```
https://localhost:3000/php-folder/php-tests/index.php
```

### The difference between the `$_SERVER['PHP_SELF']` and  `$_SERVER['REQUEST_URI']` variable

We use `$_SERVER['REQUEST_URI']` to build the full URL as it would appear in the browser's address bar. In many cases, `$_SERVER['PHP_SELF']` and  `$_SERVER['REQUEST_URI']` will return the same file path and filename so they can be used interchangeably, but they differ in the following ways:

The `$_SERVER['PHP_SELF']` variable returns the path and filename of the script that is being served. For example:

```
/php-folder/php-tests/index.php
```

The `$_SERVER['REQUEST_URI']` variable returns the URI that's used which includes any query parameters. For example:

```
/php-folder/php-tests/index.php?param=value
```

The output of `$_SERVER['PHP_SELF']` and `$_SERVER['REQUEST_URI']` will differ when using the directory of a virtual server, or when the URL is rewritten.

---

*Source: [sentry.io/answers/how-do-i-get-the-full-url-in-php/](https://sentry.io/answers/how-do-i-get-the-full-url-in-php/)*
