我在Windows Vista上使用XAMPP.在我的发展中,我有http://127.0.0.1/test_website/.
我如何http://127.0.0.1/test_website/使用PHP?
我试过这样的东西,但没有一个有用.
echo dirname(__FILE__)
or
echo basename(__FILE__);
etc.
Run Code Online (Sandbox Code Playgroud)
mač*_*ček 237
试试这个:
<?php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>
Run Code Online (Sandbox Code Playgroud)
如果您打算使用https,可以使用:
function url(){
return sprintf(
"%s://%s%s",
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
$_SERVER['SERVER_NAME'],
$_SERVER['REQUEST_URI']
);
}
echo url();
#=> http://127.0.0.1/foo
Run Code Online (Sandbox Code Playgroud)
根据这个答案,请确保正确配置您的Apache,以便您可以安全地依赖SERVER_NAME.
<VirtualHost *>
ServerName example.com
UseCanonicalName on
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)
注意:如果您依赖于HTTP_HOST键(包含用户输入),您仍然需要进行一些清理,删除空格,逗号,回车,任何不是域的有效字符.例如,检查php builtin parse_url函数.
ftr*_*ter 26
功能调整为执行时没有警告:
function url(){
if(isset($_SERVER['HTTPS'])){
$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
}
else{
$protocol = 'http';
}
return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
Run Code Online (Sandbox Code Playgroud)
SpY*_*3HH 20
有趣的'base_url'片段!
if (!function_exists('base_url')) {
function base_url($atRoot=FALSE, $atCore=FALSE, $parse=FALSE){
if (isset($_SERVER['HTTP_HOST'])) {
$http = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
$hostname = $_SERVER['HTTP_HOST'];
$dir = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
$core = preg_split('@/@', str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(dirname(__FILE__))), NULL, PREG_SPLIT_NO_EMPTY);
$core = $core[0];
$tmplt = $atRoot ? ($atCore ? "%s://%s/%s/" : "%s://%s/") : ($atCore ? "%s://%s/%s/" : "%s://%s%s");
$end = $atRoot ? ($atCore ? $core : $hostname) : ($atCore ? $core : $dir);
$base_url = sprintf( $tmplt, $http, $hostname, $end );
}
else $base_url = 'http://localhost/';
if ($parse) {
$base_url = parse_url($base_url);
if (isset($base_url['path'])) if ($base_url['path'] == '/') $base_url['path'] = '';
}
return $base_url;
}
}
Run Code Online (Sandbox Code Playgroud)
使用简单如下:
// url like: http://stackoverflow.com/questions/2820723/how-to-get-base-url-with-php
echo base_url(); // will produce something like: http://stackoverflow.com/questions/2820723/
echo base_url(TRUE); // will produce something like: http://stackoverflow.com/
echo base_url(TRUE, TRUE); || echo base_url(NULL, TRUE); // will produce something like: http://stackoverflow.com/questions/
// and finally
echo base_url(NULL, NULL, TRUE);
// will produce something like:
// array(3) {
// ["scheme"]=>
// string(4) "http"
// ["host"]=>
// string(12) "stackoverflow.com"
// ["path"]=>
// string(35) "/questions/2820723/"
// }
Run Code Online (Sandbox Code Playgroud)
小智 12
$base_url="http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["REQUEST_URI"].'?').'/';
Run Code Online (Sandbox Code Playgroud)
用法:
print "<script src='{$base_url}js/jquery.min.js'/>";
Run Code Online (Sandbox Code Playgroud)
agr*_*.in 12
$modifyUrl = parse_url($url);
print_r($modifyUrl)
Run Code Online (Sandbox Code Playgroud)
它只是简单易用
输出:
Array
(
[scheme] => http
[host] => aaa.bbb.com
[path] => /
)
Run Code Online (Sandbox Code Playgroud)
我认为$_SERVER超全球拥有您正在寻找的信息.它可能是这样的:
echo $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']
Run Code Online (Sandbox Code Playgroud)
您可以在此处查看相关的PHP文档.
小智 6
简单又容易的技巧:
$host = $_SERVER['HTTP_HOST'];
$host_upper = strtoupper($host);
$path = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$baseurl = "http://" . $host . $path . "/";
Run Code Online (Sandbox Code Playgroud)
网址如下所示:http://example.com/folder/
小智 5
你可以这样做,但抱歉我的英语不够好。
首先,使用这个简单的代码获取主页 url。
我已经在本地服务器和公共服务器上测试了这段代码,结果很好。
<?php
function home_base_url(){
// first get http protocol if http or https
$base_url = (isset($_SERVER['HTTPS']) &&
$_SERVER['HTTPS']!='off') ? 'https://' : 'http://';
// get default website root directory
$tmpURL = dirname(__FILE__);
// when use dirname(__FILE__) will return value like this "C:\xampp\htdocs\my_website",
//convert value to http url use string replace,
// replace any backslashes to slash in this case use chr value "92"
$tmpURL = str_replace(chr(92),'/',$tmpURL);
// now replace any same string in $tmpURL value to null or ''
// and will return value like /localhost/my_website/ or just /my_website/
$tmpURL = str_replace($_SERVER['DOCUMENT_ROOT'],'',$tmpURL);
// delete any slash character in first and last of value
$tmpURL = ltrim($tmpURL,'/');
$tmpURL = rtrim($tmpURL, '/');
// check again if we find any slash string in value then we can assume its local machine
if (strpos($tmpURL,'/')){
// explode that value and take only first value
$tmpURL = explode('/',$tmpURL);
$tmpURL = $tmpURL[0];
}
// now last steps
// assign protocol in first value
if ($tmpURL !== $_SERVER['HTTP_HOST'])
// if protocol its http then like this
$base_url .= $_SERVER['HTTP_HOST'].'/'.$tmpURL.'/';
else
// else if protocol is https
$base_url .= $tmpURL.'/';
// give return value
return $base_url;
}
?>
// and test it
echo home_base_url();
Run Code Online (Sandbox Code Playgroud)
输出将是这样的:
local machine : http://localhost/my_website/ or https://myhost/my_website
public : http://www.my_website.com/ or https://www.my_website.com/
Run Code Online (Sandbox Code Playgroud)
使用您网站的home_base_url功能并定义它index.php
然后你可以使用这个函数通过 url 加载脚本、CSS 和内容
<?php
echo '<script type="text/javascript" src="'.home_base_url().'js/script.js"></script>'."\n";
?>
Run Code Online (Sandbox Code Playgroud)
将创建如下输出:
<script type="text/javascript" src="http://www.my_website.com/js/script.js"></script>
Run Code Online (Sandbox Code Playgroud)
如果这个脚本工作正常,,!
以下代码将减少检查协议的问题。$_SERVER['APP_URL'] 将显示带有协议的域名
$_SERVER['APP_URL'] 将返回protocol://domain(例如:- http://localhost)
$_SERVER['REQUEST_URI'] 用于 url 的其余部分,例如/directory/subdirectory/something/else
$url = $_SERVER['APP_URL'].$_SERVER['REQUEST_URI'];
Run Code Online (Sandbox Code Playgroud)
输出将是这样的
http://localhost/directory/subdirectory/something/else
试试下面的代码:
$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
echo $config['base_url'];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
522203 次 |
| 最近记录: |