Get the complete url of the current web page with PHP
Posted in: PROGRAMMING, UNCATEGORIZEDGoing to share with you a small snippet today, to determine and get the complete url of the current page in PHP 1. The Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
function getPageUrl() { // First we see if the request was over SSL (HTTPS) if( isset( $_SERVER['HTTPS'] ) && !empty( $_SERVER['HTTPS'] ) && ( strtolower( $_SERVER['HTTPS'] ) != 'off' ) ) $protocol = 'https'; else $protocol = 'http'; $url = $protocol.'://'; // Now check for the port if ($_SERVER["SERVER_PORT"] != "80") { $url .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $url .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $url; } |
2. Usage
1 2 3 4 |
//... $url = getPageUrl(); echo $url; //... |