PHP
Web Development
Coding
URL Manipulation
PHP Tutorials

Get the full URL in PHP

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In PHP, obtaining the full URL of the current page can be crucial for many purposes such as redirection, logging, or creating dynamic links. We can achieve this through a combination of server variables that PHP provides. The approach can vary slightly depending on whether your server is using HTTP or HTTPS, so the solution needs to handle both cases.

Overview of Server Variables

PHP provides several server variables accessible via the $_SERVER superglobal array. They are useful in constructing the full URL:

  • $_SERVER['HTTPS']: Indicates if the connection is over HTTPS (on if yes).
  • $_SERVER['HTTP_HOST']: Contains the domain name or IP address (including the port if included) from the request.
  • $_SERVER['SERVER_PORT']: Provides the port number on which the request was received.
  • $_SERVER['REQUEST_URI']: Contains the URI which was given in order to access the page. For instance, '/index.php'.

Constructing the Full URL

Here is a function that composes the full URL based on these server variables:

php
1function getFullURL() {
2    // Determine the protocol (http or https)
3    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://";
4    
5    // Build the full URL
6    $url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
7    
8    return $url;
9}
10
11echo getFullURL();

This function first checks if HTTPS is enabled or if the port used is 443 (commonly HTTPS). It then constructs the URL using the determined protocol, the HTTP host, and the request URI.

Handling Edge Cases

While the above function works well in many cases, there are a few edge cases:

  1. Reverse Proxies and Load Balancers: Often, especially in cloud deployments, applications are behind a load balancer or reverse proxy. These tools may terminate HTTPS connections at their level, making the $_SERVER['HTTPS'] not reliable.
    • You can account for these by also checking headers possibly added by your proxy, like X-Forwarded-Proto.
php
   $protocol = (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') ? "https://" : "http://";
  1. Non-standard Ports: If your application runs on a non-standard port, you might want to include this in the URL explicitly.
    • Check if a custom port is used and append if necessary.
php
1   $port = $_SERVER['SERVER_PORT'];
2   $url = $protocol . $_SERVER['HTTP_HOST'];
3   $url .= (($protocol === "http://" && $port != 80) || ($protocol === "https://" && $port != 443)) ? ":$port" : "";
4   $url .= $_SERVER['REQUEST_URI'];

Summary Table

Key $_SERVER VariableDescription
HTTPSIndicates if HTTPS is used, typically returns on.
HTTP_HOSTThe hostname (and possibly port number) of the server.
SERVER_PORTThe port on the server the request was received on.
REQUEST_URIThe URI of the requested page.

Additional Considerations

  • Security: Be cautious of data from $_SERVER as it may be manipulable by users, potentially leading to security issues like HTTP Host Header attacks. Validating and sanitizing inputs is key.
  • Portability: Note that server configurations and PHP environments can vary. Always test URL generation in the specific environment where your application is hosted.

By using PHP's superglobal $_SERVER, developers can dynamically generate complete URLs, which can accommodate various server configurations and HTTP headers. This utility supports many common tasks such as generating callback URLs for OAuth processes, creating redirect URLs, and more, making it an essential technique for robust PHP development.


Course illustration
Course illustration

All Rights Reserved.