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
🌐
PHP
php.net › manual › en › ref.url.php
PHP: URL Functions - Manual
urlencode — URL-encodes string · Learn How To Improve This Page • Submit a Pull Request • Report a Bug · +add a note · up · down · -1 chemanfit at hotmail ¶ · 23 years ago · just a side note to the above you will need to add the ? example $page=$PHP_SELF."?".$_SERVER['QUERY_STRING']; +add a note ·
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

🌐
EDUCBA
educba.com › home › software development › software development tutorials › php tutorial › php url
PHP URL | Examples to Demonstrate how PHP URL Works?
April 15, 2023 - You can check the output so that you will under how different types of URL’s are encoded. <br> tags with echo statement just to display line breaks and <hr> tags are used to display horizontal lines when the syntax is executed. ... <?php // This is the PHP program of illustrating the urlencode functionality echo urlencode("https://www.educba.com") .
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
GeeksforGeeks
geeksforgeeks.org › php › get-the-full-url-in-php
Get the full URL in PHP - GeeksforGeeks
July 14, 2025 - $_SERVER['PHP_SELF']; // Display the complete URL echo $link; ?>
🌐
Tutorialspoint
tutorialspoint.com › php › php_urls_functions.htm
PHP - URL Functions
Since PHP URL functions are built into the PHP core, no further installation is required.
🌐
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
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-get-current-page-url-in-php.php
How to Get Current Page URL in PHP
"https://" : "http://"; $url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; echo $url; // Outputs: Full URL $query = $_SERVER['QUERY_STRING']; echo $query; // Outputs: Query String ?>
Find elsewhere
🌐
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
🌐
W3Schools
w3schools.com › php › php_superglobals_get.asp
PHP $_GET Superglobal
PHP superglobals are built-in variables that are always accessible in all scopes! There are two main ways to send variables via the HTTP GET method: ... A query string is data added to the end of a URL. In the <a> element below, everything after the ?
🌐
GitHub
github.com › wasinger › url
GitHub - wasinger/url: PHP class for handling and manipulating URLs · GitHub
use \Wa72\Url\Url; $url = new Url('http://my-server.com/index.php?p1=foo&p2=bar'); // or alternatively use the static factory function `parse`: $url = Url::parse('http://my-server.com/index.php?p1=foo&p2=bar'); // set another host $url->setHost('another-server.org'); // return the URL as string again echo $url->write(); // or simply: echo $url;
🌐
YouTube
youtube.com › joshua herbison
Get the full URL in PHP - PHP Tutorial - Get the URL if you are not able to use $_GET - YouTube
In this video we use $_SERVER to put together the full URL. This was a question asked by a viewer that mentioned the URL was masked by an Apache .htaccess fi...
Published: August 12, 2021
Views: 1K
🌐
SitePoint
sitepoint.com › php
How to add a URL (link) in PHP code? - PHP - SitePoint Forums | Web Development & Design Community
March 13, 2007 - but you’d only have to escape single quotes, and they’re pretty rare in URL’s oh, and it’s better on the PHP parser because it doesn’t have to search for varibles in the string.
🌐
Reintech
reintech.io › blog › understanding-implementing-php-parse-url-function
Understanding and Implementing PHP's `parse_url()` Function
April 14, 2023 - PHP's parse_url() function dissects URLs into their constituent parts, providing structured access to components like scheme, host, port, path, query string, and fragment.
🌐
YouTube
youtube.com › mike dane
URL Parameters | PHP | Tutorial 13 - YouTube
Giraffe Academy is rebranding! I've decided to re-focus the brand of this channel to highlight myself as a developer and teacher! The newly minted Mike Dane ...
Published: November 15, 2017
Views: 9K