使用$_SERVER['HTTP_HOST']变量只获取到域名:
$domain = $_SERVER['HTTP_HOST'];
echo $domain;
获取包含协议和域名的完整URL
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://';
$domain = $_SERVER['HTTP_HOST'];
$current_url = $protocol . $domain;
echo $current_url;