Get current URL in PHP

# Function
function get_current_url($ignore_port_80 = true) {
  $is_https = $_SERVER['HTTPS'] === 'on';
  $show_port = $_SERVER['SERVER_PORT'] !== '80' || !$ignore_port_80;
 
  $url = ''
    . 'http'
    . ($is_https ? 's' : '')
    . '://'
    . $_SERVER['SERVER_NAME']
    . ($show_port ? (':'.$_SERVER['SERVER_PORT']) : '')
    . $_SERVER['REQUEST_URI'];
  return $url;
}
 
# Examples
echo get_current_url();       # http://www.kadimi.com/en/some-title-123
echo get_current_url(true);   # same as above
echo get_current_url(false);  # http://www.kadimi.com:80/en/some-title-123
Processing your request, Please wait....

No Responses to “Get current URL in PHP”

Comments (Your Comments)

Leave a Reply

You must be logged in to post a comment.