Last active
December 19, 2015 19:18
-
-
Save andrewspear/6004632 to your computer and use it in GitHub Desktop.
A simple PHP function to fetch the current page URL with PHP, .htaccess rewrites are respected and optionally you can exclude the query string, useful for creating canonical URLs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
function currentPageURL($includeQueryString = true) { | |
$pageURL = 'http'; | |
$pageURL .= ($_SERVER["HTTPS"] == "on") ? "s" : ""; | |
$pageURL .= "://"; | |
$pageURL .= ($_SERVER["SERVER_PORT"] != "80") ? $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"] : $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; | |
if (!$includeQueryString) { | |
$pageURL = str_replace('?'.$_SERVER["QUERY_STRING"], '', $pageURL); | |
} | |
return $pageURL; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment