burger menu icon
WillMaster

WillMaster > LibraryWeb Content Preparation

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Building a URL to Self With PHP

Today's article is technical.

When a PHP page is requested from the server, the PHP code runs before the result is sent to the browser. Thus, there is no URL available in a browser while PHP runs.

Although the URL in the browser's address bar is unavailable, PHP does store values that can be used to construct what the URL will be.

Each of the parts resides in the global $_SERVER variable. This list names them along with a hint about what they contain.

  • $_SERVER['HTTPS'] (protocol test)
  • $_SERVER['HTTP_HOST'] (domain & port)
  • $_SERVER['PHP_SELF'] (relative URL)
  • $_SERVER['QUERY_STRING'] (URL parameters)

Let's go ahead and build the full, absolute URL of the current PHP script, which would be the URL of the web page where this article is published.

$_SERVER['HTTPS']

This variable is used to test whether the protocol part of the URL is http or https.

First, we'll do the test. Then we'll construct the beginning of the URL.

<?php
$protocol = isset($_SERVER['HTTPS']) ? 'https' : 'http';
$URL = $protocol . "://";
echo($URL);
?>

Here is the output when the above code is run on the web page where this article is published.

https://

$_SERVER['HTTP_HOST']

This variable contains the current host information. That would be the domain name and, perhaps, also the port number. If the port is not default (80 or 443), then a colon and the port number will be appended to the domain name.

Let's append the host information to the URL we started above.

<?php
$protocol = isset($_SERVER['HTTPS']) ? 'https' : 'http';
$URL = $protocol . "://";
$URL .= $_SERVER['HTTP_HOST'];
echo($URL);
?>

Here is the live output. If a port is specified in the URL of this web page, it will be included in this output.

https://www.willmaster.com

$_SERVER['PHP_SELF']

This variable contains the relative URL. (If all you need is a relative URL, not an absolute URL, then you won't need the first two variables already discussed.)

Let's append the relative URL to the example URL variable.

<?php
$protocol = isset($_SERVER['HTTPS']) ? 'https' : 'http';
$URL = $protocol . "://";
$URL .= $_SERVER['HTTP_HOST'];
$URL .= $_SERVER['PHP_SELF'];
echo($URL);
?>

https://www.willmaster.com/library/web-content-prep/building-a-url-to-self-with-php.php

$_SERVER['QUERY_STRING']

If the URL in the browser's address bar has a parameter (i.e., ?name=Will&month=Apr), the information will be in the $_SERVER['QUERY_STRING'] variable. The "?" character won't be in the $_SERVER['QUERY_STRING'] variable, so the variable will need to be tested before printing the "?."

<?php
$protocol = isset($_SERVER['HTTPS']) ? 'https' : 'http';
$URL = $protocol . "://";
$URL .= $_SERVER['HTTP_HOST'];
$URL .= $_SERVER['PHP_SELF'];
if(isset($_SERVER['QUERY_STRING']) and $_SERVER['QUERY_STRING'])
{ $URL .=  "?{$_SERVER['QUERY_STRING']}"; }
echo($URL);
?>

Below is the $URL value of the above code as it applies to the web page where this article is printed. If you appended ?name=Will&month=Apr to the URL in the browser's address bar and reloaded, you'll see the parameter information below.

https://www.willmaster.com/library/web-content-prep/building-a-url-to-self-with-php.php

As you've seen above, the URL that will be in the browser's address bar when the page is loaded can be constructed ahead of time with PHP code.

Use the four mentioned $_SERVER variables to determine the URL. If only part of the URL is needed, then, well, use only those $_SERVER variables.

(This content first appeared in Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2026 Will Bontrager Software LLC