php获取当前主机域名

文章描述:

PHP,JavaScript 获取当前域名

访问地址:http://localhost:10003/index.php

<?php
$host = $_SERVER['HTTP_HOST'];
echo "当前域名为:" . $host;

结果:当前域名为:localhost:10003

 

获取包含网址协议的当前域名

echo domain();
/** 
* 获取包含网址协议的域名 
*/ 
function domain() 
{ 
	$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https' : 'http'; 
	return $http_type . '://' . $_SERVER['HTTP_HOST']; 
}

结果:http://localhost:10003

 

 

JavaScript 获取包含网址协议的当前域名

var domain = location.protocol + '//' + location.host
console.log(domain)

结果 :http://localhost:9527

 

给图片添加http

$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https' : 'http';
$url = $http_type . '://' . $_SERVER['HTTP_HOST'].$path;
$fileurl = str_replace('\\','/',$url);

 

发布时间:2023/11/12

发表评论