获取当前域名
function get_host(){ $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = $protocol.$_SERVER['HTTP_HOST']; return $url; }
1、先判断当前协议是否为https还是http
2、拼接上当前域名
得到结果,如:https://www.liqingbo.cn/
分析:
print_r($_SERVER); //输出: Array ( [PATH] => C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows7Master;d:\composer;C:\Program Files\TortoiseSVN\bin;D:\phpstudy_pro\Extensions\php\php7.3.4nts;C:\composer;D:\redis;D:\Program Files\PuTTY\;D:\phpstudy_pro\Extensions\MySQL5.7.26\bin;C:\Go\bin;D:\go\bin;D:\Program Files\nodejs\; [SYSTEMROOT] => C:\Windows [COMSPEC] => C:\Windows\system32\cmd.exe [PATHEXT] => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC [WINDIR] => C:\Windows [PHPRC] => D:/phpstudy_pro/Extensions/php/php7.3.4nts [_FCGI_SHUTDOWN_EVENT_] => 8628 [ORIG_PATH_TRANSLATED] => D:\phpServer\www\shop\index.php\api\login\test [ORIG_SCRIPT_FILENAME] => D:/phpServer/www/shop/index.php [ORIG_PATH_INFO] => /index.php/api/login/test [PATH_TRANSLATED] => D:/phpServer/www/shop/api/login/test [PATH_INFO] => /api/login/test [SCRIPT_NAME] => /index.php [REQUEST_URI] => /api/login/test [QUERY_STRING] => [REQUEST_METHOD] => GET [SERVER_PROTOCOL] => HTTP/1.1 [GATEWAY_INTERFACE] => CGI/1.1 [REDIRECT_URL] => /api/login/test [REMOTE_PORT] => 63921 [SCRIPT_FILENAME] => D:\phpServer\www\shop\index.php [SERVER_ADMIN] => admin@example.com [CONTEXT_DOCUMENT_ROOT] => D:/phpServer/www/shop [CONTEXT_PREFIX] => [REQUEST_SCHEME] => http [DOCUMENT_ROOT] => D:/phpServer/www/shop [REMOTE_ADDR] => 127.0.0.1 [SERVER_PORT] => 80 [SERVER_ADDR] => 127.0.0.1 [SERVER_NAME] => shop.bendi.com [SERVER_SOFTWARE] => Apache/2.4.39 (Win64) OpenSSL/1.1.1b mod_fcgid/2.3.9a mod_log_rotate/1.02 [SERVER_SIGNATURE] => [SystemRoot] => C:\Windows [HTTP_COOKIE] => thinkphp_show_page_trace=0|0; PHPSESSID=e6e7f83f87766b0b664239607d58932a [HTTP_ACCEPT_LANGUAGE] => zh-CN,zh;q=0.9 [HTTP_ACCEPT_ENCODING] => gzip, deflate [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36 [HTTP_UPGRADE_INSECURE_REQUESTS] => 1 [HTTP_CACHE_CONTROL] => max-age=0 [HTTP_CONNECTION] => close [HTTP_HOST] => www.liqingbo.cn [REDIRECT_STATUS] => 200 [FCGI_ROLE] => RESPONDER [PHP_SELF] => /index.php/api/login/test [REQUEST_TIME_FLOAT] => 1590063692.7944 [REQUEST_TIME] => 1590063692 )
获取当前端口号
$_SERVER['SERVER_PORT']
获取域名
[HTTP_HOST] => www.liqingbo.cn