There are number of times when we need to play with PHP and the pre-defined constants and variables. PHP has a vast number of methods which can help us in dealing with almost all issues. Today, I am gonna share with the trick to find out current page’s URL, yes the link that you see in your browser’s address bar.
Here is the code:
function getCurrentPageUrl(){
//All URLs start with http: followed by 's' (in case of https)
$currentPageURL = 'http';
//check if the server is https based
if ($_SERVER["HTTPS"] == 'on') {
//attach extra 's' for https based URLs
$currentPageURL .= 's';
}
//attach characetrs needed to form a valid URL
$currentPageURL .= '://';
//get full script URL from SERVER array
$currentPageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
//return the exact current page URL
return $currentPageURL;
}
//Usage:
echo getCurrentPageUrl();
The above code works for both http and https based URL’s. To find out more about URL’s, look at here.
Comments are welcome and if you have any tips, code related to this issue, please share it with us in comments.





No comments yet.