Php 返回安装脚本的URL
我需要动态地找出脚本的基本url。假设我在文件夹脚本中安装了一个脚本,脚本的基本url是 ,假设我把它放在根中,它必须是 有没有办法动态地找出这个问题 使用Php 返回安装脚本的URL,php,Php,我需要动态地找出脚本的基本url。假设我在文件夹脚本中安装了一个脚本,脚本的基本url是 ,假设我把它放在根中,它必须是 有没有办法动态地找出这个问题 使用basename(\uuuu DIR\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu 尝试了类似于echo$\u服务器['SERVER\u NAME']的东西。目录名($_服务器['REQU
basename(\uuuu DIR\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
尝试了类似于echo$\u服务器['SERVER\u NAME']的东西。目录名($_服务器['REQUEST_URI'])代码>
但这当然总是返回当前页面的url
我想在我的index.php中声明一个名为ROOT_URL的常量(所有内容都由该文件加载),这样我就可以在任何希望的项目中使用该常量。您是否尝试过$script_filename=$\u SERVER['script_filename']代码>
您可以在这里查看一下几周前,我在MVC实现中也谈到了同样的问题
好了:
public static function RealDirname($path)
{
$path = dirname($path);
if($path[0] == '\\') //Windows servers tend to mess up
$path[0] = '/';
$lastChar = strlen($path) - 1;
if($path[$lastChar] != '/')
$path .= '/';
return $path;
}
public static function GetAbsolutePath()
{
global $config;
if(empty($config["base_path"]))
$config["base_path"] = self::GetDomainURL();
return $config["base_path"].(self::RealDirname($_SERVER['PHP_SELF']));
}
public static function GetDomainURL()
{
return (isset($_SERVER['HTTPS']) ? 'https://' :'http://').$_SERVER['HTTP_HOST'];
}
只需调用getAbsolutePath,它就会返回整个路径,并带有URL:)
在位于以下位置的页面上尝试:
http://localhost:8080/old/test.php
返回的GetAbsolutePath:
http://localhost:8080/old/
和GetDomainURL:
http://localhost:8080
在安东尼的帮助下修好了
稍微改变了他第一次提交的方法:
public static function RealDirname($path)
{
$path = dirname($path);
if($path[0] == '\\') //Windows servers tend to mess up [everything]
$path[0] = '/';
$lastChar = strlen($path) - 1;
if($path[$lastChar] != '/')
$path .= '/';
return $path;
}
public static function getAbsolutePath()
{
$path = (isset($_SERVER['HTTPS']) ? 'https://' :'http://').$_SERVER['HTTP_HOST'];
$path = $path.self::RealDirname($_SERVER['PHP_SELF']);
return substr($path, 0, strpos($path, "index.php"));
}
去掉index.php之后的所有内容并返回根目录。可能是m8的副本,其中定义了enkelDirname:p?糟糕,这是一个输入错误,用RealDirname替换它;)尝试了m8,当我使用您的方法在index.php上定义常量时,它返回以下内容:http://www.domain.com/index.php/user/
当im访问http://www.domain.nl/index.php/user/register
它应该只返回http://www.domain.com
好的,但是当我安装脚本时,让我们把它放在名为mvc
的文件夹中,它仍然会返回http://www.domain.com
,而不是http://www.domain.com/mvc/
,问题是我可以在代码中添加或添加一些硬的内容,但我真的很喜欢这样做。我现在提供的内容只是返回域URL,在返回脚本路径之前发布的内容,例如: