
How to Get Current Page URL using PHP
This php snippet prints current page url. It accepts a Boolean parameter as TRUE/FALSE which acts as a flag for displaying query string. It returns the current site URL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/* * Fetches current URL via php * @$queryString = FALSE / TRUE * @returns the current page url */ function getCurrentUrl($queryString = TRUE) { $protocol = 'http'; if ($_SERVER['SERVER_PORT'] == 443 || (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')) { $protocol .= 's'; $protocolPort = $_SERVER['SERVER_PORT']; } else { $protocolPort = 80; } $host = $_SERVER['HTTP_HOST']; $port = $_SERVER['SERVER_PORT']; $request = $_SERVER['PHP_SELF']; $query = ($queryString === TRUE) ? $_SERVER['QUERY_STRING'] : ""; $url = $protocol . '://' . $host . ($port == $protocolPort ? '' : ':' . $port) . $request . (empty($query) ? '' : '?' . $query); return $url; } |
How …
Read More