Get entire URL including query string in PHP

<?php
// Get current protocol.
$protocol = stripos($_SERVER['SERVER_PROTOCOL'],'https') === 0 ? 'https://' : 'http://';
// Get current domain.
$domain = $_SERVER['HTTP_HOST'];
// Get current path to the current file.
$path = $_SERVER['SCRIPT_NAME'];
// Get current querystring.
$queryString = $_SERVER['QUERY_STRING'];
// Merge all the variables together. 
$url = $protocol . $domain . $path . "?" . $queryString;
echo $url

Leave a Comment

Your email address will not be published. Required fields are marked *