Have a look at $_SERVER['REQUEST_URI'], i.e.

$actual_link = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

(Note that the double quoted string syntax is perfectly correct.)

If you want to support both HTTP and HTTPS, you can use

$actual_link = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

โš ๏ธ This code has security implications because the client and the server can set HTTP_HOST and REQUEST_URI to arbitrary values. It is absolutely necessary to sanitize both values and do proper input validation (CWE-20). They must not be used in any security context.

Answer from ax. on Stack Overflow
Top answer
1 of 16
2560

Have a look at $_SERVER['REQUEST_URI'], i.e.

$actual_link = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

(Note that the double quoted string syntax is perfectly correct.)

If you want to support both HTTP and HTTPS, you can use

$actual_link = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

โš ๏ธ This code has security implications because the client and the server can set HTTP_HOST and REQUEST_URI to arbitrary values. It is absolutely necessary to sanitize both values and do proper input validation (CWE-20). They must not be used in any security context.

2 of 16
489

Short version to output link on a webpage

$url =  "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
echo '<a href="' . $escaped_url . '">' . $escaped_url . '</a>';

Here are some more details about the issues and edge cases of the //example.com/path/ format

Full version

function url_origin( use_forwarded_host = false )
{
    $ssl      = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
    $sp       = strtolower( $s['SERVER_PROTOCOL'] );
    $protocol = substr( $sp, 0, strpos( ssl ) ? 's' : '' );
    $port     = $s['SERVER_PORT'];
    $port     = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
    $host     = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
    $host     = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
    return $protocol . '://' . $host;
}

function full_url( use_forwarded_host = false )
{
    return url_origin( use_forwarded_host ) . $s['REQUEST_URI'];
}

$absolute_url = full_url( $_SERVER );
echo $absolute_url;

This is a heavily modified version of http://snipplr.com/view.php?codeview&id=2734 (Which no longer exists)

URL structure:

scheme://username:password@domain:port/path?query_string#fragment_id

The parts in bold will not be included by the function

Notes:

  • This function does not include username:password from a full URL or the fragment (hash).
  • It will not show the default port 80 for HTTP and port 443 for HTTPS.
  • Only tested with http and https schemes.
  • The #fragment_id is not sent to the server by the client (browser) and will not be added to the full URL.
  • $_GET will only contain foo=bar2 for an URL like /example?foo=bar1&foo=bar2.
  • Some CMS's and environments will rewrite $_SERVER['REQUEST_URI'] and return /example?foo=bar2 for an URL like /example?foo=bar1&foo=bar2, use $_SERVER['QUERY_STRING'] in this case.
  • Keep in mind that an URI = URL + URN, but due to popular use, URL now means both URI and URL.
  • You should remove HTTP_X_FORWARDED_HOST if you do not plan to use proxies or balancers.
  • The spec says that the Host header must contain the port number unless it is the default number.

Client (Browser) controlled variables:

  • $_SERVER['REQUEST_URI']. Any unsupported characters are encoded by the browser before they are sent.
  • $_SERVER['HTTP_HOST'] and is not always available according to comments in the PHP manual: http://php.net/manual/en/reserved.variables.php
  • $_SERVER['HTTP_X_FORWARDED_HOST'] gets set by balancers and is not mentioned in the list of $_SERVER variables in the PHP manual.

Server controlled variables:

  • $_SERVER['HTTPS']. The client chooses to use this, but the server returns the actual value of either empty or "on".
  • $_SERVER['SERVER_PORT']. The server only accepts allowed numbers as ports.
  • $_SERVER['SERVER_PROTOCOL']. The server only accepts certain protocols.
  • $_SERVER['SERVER_NAME'] . It is set manually in the server configuration and is not available for IPv6 according to kralyk.

Related:

What is the difference between HTTP_HOST and SERVER_NAME in PHP?
Is Port Number Required in HTTP "Host" Header Parameter?
https://stackoverflow.com/a/28049503/175071

๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.parse-url.php
PHP: parse_url - Manual
This is based on RFC 2396 section 5.2. */ function resolve_url($base, $url) { if (!strlen($base)) return $url; // Step 2 if (!strlen($url)) return $base; // Step 3 if (preg_match('!^[a-z]+:!i', $url)) return $url; $base = parse_url($base); if ($url{0} == "#") { // Step 2 (fragment) $base['fragment'] = substr($url, 1); return unparse_url($base); } unset($base['fragment']); unset($base['query']); if (substr($url, 0, 2) == "//") { // Step 4 return unparse_url(array( 'scheme'=>$base['scheme'], 'path'=>$url, )); } else if ($url{0} == "/") { // Step 5 $base['path'] = $url; } else { // Step 6 $path =
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ get-the-full-url-in-php
Get the full URL in PHP - GeeksforGeeks
July 14, 2025 - In this article, we will see how to get the complete URL of currently running pages using PHP, along with understanding their implementation through the examples. $_SERVER is a super global variable in PHP that contains the details related to the header, paths, and script locations.
๐ŸŒ
SWHarden
swharden.com โ€บ blog โ€บ 2021-01-28-php-directory-url
Get URL of a Directory with PHP
// This script displays the URL of the current folder $realDocRoot = realpath($_SERVER['DOCUMENT_ROOT']); $realDirPath = realpath(__DIR__); $suffix = str_replace($realDocRoot, '', $realDirPath); $prefix = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; $folderUrl = $prefix ...
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ php โ€บ how do i get the full url in php?
PHP Get Full Current Page URL with $_SERVER Variables | Sentry
July 31, 2026 - 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
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ php-get-url-how-to-get-the-full-url-of-the-current-page
PHP Get URL โ€“ How to Get the Full URL of the Current Page
June 22, 2020 - One of these is, of course, the current URL. PHP stores these pieces of useful information in its array of super-global variables.
๐ŸŒ
Simplilearn
simplilearn.com โ€บ home โ€บ resources โ€บ software development โ€บ how to get full url in php - a step-by-step guide
How to Get Full URL in PHP - A Step-by-Step Guide
September 10, 2025 - Uncover three methods to get a full URL in PHP. PHP scripts retrieve URLs of web pages from web servers. Learn how to get a full URL step by step. Start now!
Address: 5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
Find elsewhere
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-get-current-page-url-in-php.php
How to Get Current Page URL in PHP
PHP Array Functions PHP String Functions PHP File System Functions PHP Date/Time Functions PHP Calendar Functions PHP MySQLi Functions PHP Filters PHP Error Levels ... You can use the $_SERVER built-in variable to get the current page URL in PHP.
๐ŸŒ
Pi My Life Up
pimylifeup.com โ€บ home โ€บ how to get the current page url in php
How to Get the Current Page URL in PHP - Pi My Life Up
May 3, 2022 - This guide will teach you how to get the current page URL in PHP. This involves using data from the $_SERVER global variable.
๐ŸŒ
Nabilhassen
nabilhassen.com โ€บ how-to-read-and-get-url-parameters-in-php
How to Read and Get URL Parameters in PHP - Nabil Hassen
September 24, 2025 - The simplest and most common way to access URL parameters in PHP is via the superglobal $_GET.
๐ŸŒ
Phpf1
phpf1.com โ€บ tutorial โ€บ get-current-page-url.html
PHP get current page URL - PhpF1.com
December 15, 2020 - You can get every piece of information about the current URL using the $_SERVER superglobal array. You can get the domain name, the script name, the URL parameters, and any variation of these.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-get-the-base-url-with-php
How to get the Base URL with PHP ? - GeeksforGeeks
July 23, 2025 - Using parse_url() The $_SERVER is a super global variable in PHP that contains the details related to the header, paths, and script locations. The base URL can be constructed using information from the $_SERVER super global array, which provides ...
๐ŸŒ
Techfry
techfry.com โ€บ home โ€บ php tutorial โ€บ how to get url of current page in php
How To Get URL of Current Page in PHP - Tech Fry
June 12, 2021 - If you have enabled mod_rewrite, you can also use $_SERVER['SCRIPT_URI'] to get the whole URL at once. The only drawback of this method is that you will get the URL without the query string.
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_superglobals_get.asp
PHP $_GET Superglobal
The $_GET superglobal contains an array of variables received via the HTTP GET method.
๐ŸŒ
Squash
squash.io โ€บ how-to-get-the-full-url-in-php
How To Get The Full URL In PHP - Squash Labs
December 4, 2023 - One way to get the full URL in PHP is by using server variables such as $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'], and $_SERVER['HTTPS']. These variables provide information about the server, the request, and the protocol being used.
๐ŸŒ
Learning About Electronics
learningaboutelectronics.com โ€บ Articles โ€บ How-to-get-the-current-page-url-of-a-web-page-using-PHP.php
How to Get the Current Page URL of a Web Page Using PHP
To get the full URL of the current page, which takes the form http://www.website.com/Directory/pagename.html, you can use the following code below. The $_SERVER['HTTP_HOST'] gets the www.website.com name of the website in use.