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

