Get the full URL in PHP
PHP program to get complete URL of currently running pages.
There are few steps to get the complete URL of the currently running page which are given below:
- Create a PHP variable which will store the URL in string format.
- Check whether the HTTPS is enabled by the server .If it is, append “https” to the URL string. If HTTPS is not enabled, append “http” to the URL string.
- Append the regular symbol, i.e. “://” to the URL.
- Append the HTTP_HOST(The host to which we have requested, e.g. www.google.com, www.yourdomain.com, etc…) name of the server.
- Append the REQUEST_URI(The resource which we have requested, e.g. /index.php, etc…) to the URL string.
Note: Use isset() function to check whether HTTPS is enable or not. The isset() function is used to check a variable exists or not.
The status of HTTPS is saved in the Global variable $_SERVER[‘HTTPS’]. So, use $_SERVER[‘HTTPS’] in isset() function is used to check whether it exists or not. This will tell us whether HTTPS is enabled or not. Check the value of $_SERVER[‘HTTPS’]. If it is “on”, then HTTPS is enabled and we have to append “https” to the URL.
Program 1:
<?php // Program to display URL of current page. if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') $link = "https"; else $link = "http"; // Here append the common URL characters. $link .= "://"; // Append the host(domain name, ip) to the URL. $link .= $_SERVER['HTTP_HOST']; // Append the requested resource location to the URL $link .= $_SERVER['REQUEST_URI']; // Print the link echo $link; ?> |
Output:
https://ide.geeksforgeeks.org/
Program 2:
<?php // Program to display current page URL. $link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; echo $link; ?> |
Output:
https://ide.geeksforgeeks.org/
The output of the above code is https://ide.geeksforgeeks.org/ instead of https://ide.geeksforgeeks.org/index.php. In order to fix this problem, need to replace, $_SERVER[‘REQUEST_URI’] with $_SERVER[‘PHP_SELF’]
Program 3: Displaying the currently executing PHP file URL
<?php // Program to display complete URL if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') $link = "https"; else $link = "http"; // Here append the common URL // characters. $link .= "://"; // Append the host(domain name, // ip) to the URL. $link .= $_SERVER['HTTP_HOST']; // Append the requested resource // location to the URL $link .= $_SERVER['PHP_SELF']; // Display the link echo $link; ?> |
Output:
https://ide.geeksforgeeks.org/index.php
Program 4:
<?php // Program to display complete URL $link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; // Display the complete URL echo $link; ?> |
Output:
https://ide.geeksforgeeks.org/index.php
Recommended Posts:
- What is full stack development ?
- How to get the file name from full path using JavaScript ?
- HTML | Responsive full page image using CSS
- How to xdebug var_dump to display full object/array ?
- How to Become a Full Stack Web Developer in 2019 : A Complete Guide
- How to detect operating system on the client machine using JavaScript ?
- What is the difference between children and childNodes in JavaScript?
- 5 Must Have Tools For Web Application Penetration Testing
- How to create hash from string in JavaScript ?
- TCS Digital Interview through Codevita'19
- Kotlin mutableSetOf() method
- Kotlin Map : mapOf()
- Kotlin Set : setOf()
- Kotlin list : listOf()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



