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.
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