PHP Exercises : Get the full URL
22. Get Full URL
Write a PHP script to get the full URL.
Sample Solution:
PHP Code:
<?php
// Constructing the full URL using $_SERVER variables
$full_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
// Display the constructed full URL
echo $full_url . "\n";
?>
Explanation:
- Constructing the Full URL:
- The code constructs the full URL by concatenating several $_SERVER variables:
- $_SERVER['HTTP_HOST'] retrieves the host name (e.g., www.example.com).
- $_SERVER['REQUEST_URI'] retrieves the URI (the path and query string) of the current request (e.g., /path/to/page?query=string).
- The full URL is formed as "http://<host><uri>" and stored in the variable $full_url.
- Display the Full URL:
- The echo statement outputs the constructed full URL, followed by a newline (\n).
Output:
View the output in the browser
Flowchart:

For more Practice: Solve these Related Problems:
- Write a PHP script to construct the full URL of the current page, including protocol, host, and query string.
- Write a PHP script to retrieve and display the full URL, then verify its format against a regular expression.
- Write a PHP script to compare the current full URL with a predefined URL and log any differences.
- Write a PHP script to extract individual URL components from the full URL and then rebuild the complete URL.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a PHP function to test whether a number is greater than 30, 20 or 10 using ternary operator.
Next: Write a PHP script to compare the PHP version.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.