Php 如何在本地主机上获取主url?
所以我在本地主机上创建了一个网站,文件夹名为“veco”,因此我的URL链接是 我当前正在使用此代码获取主url“”:Php 如何在本地主机上获取主url?,php,localhost,Php,Localhost,所以我在本地主机上创建了一个网站,文件夹名为“veco”,因此我的URL链接是 我当前正在使用此代码获取主url“”: 现在我在“关于”页面上,我的URL现在是 现在在关于页面上,我做了一个表格 <form action="data/cs_menu.php" method="POST"> <input type="text" name="f_menu"> <input type="submit" name="save_menu"> <
现在我在“关于”页面上,我的URL现在是
现在在关于页面上,我做了一个表格
<form action="data/cs_menu.php" method="POST">
<input type="text" name="f_menu">
<input type="submit" name="save_menu">
</form>
现在,当我提交此表单时,它会将我重定向到正确的位置,但当我使用home_url函数时,它会给我“”而不是“”
有什么想法吗?我现在不使用Wordpress,但是如果您熟悉Wordpress,它们有一个函数“home_url();”,它返回我要复制的“”
我现在不使用Wordpress,但如果您熟悉Wordpress,它们有函数“home_url();”
WordPress从数据库中查找值,在安装WordPress时在数据库中设置该值
通常,无法从网站上的任意页面确定主页的URL。最接近的方法是手动向每个页面添加一条规则,内容是“主页正好位于该页面上方的两个目录”。您可以尝试以下方法:
<?php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>
这样说吧:
echo url();
#=> http://127.0.0.1/foo
对于$\u服务器全局变量,可以找到文档
原始答案已发布在网站上
你应该检查一下,让我知道这是否解决了你的问题。
希望这有帮助。伙计们,我终于找到了解决方案,它也是动态的。我只编辑了我的函数并添加了以下内容:
$home_url = (explode("/",$full_url));
return $home_url[0]."//".$home_url[2]."/".$home_url[3];
下面是完整的最终代码:
function home_url()
{
// output: /myproject/index.php
$currentPath = $_SERVER['PHP_SELF'];
// output: Array ( [dirname] => /myproject [basename] => index.php [extension] => php [filename] => index )
$pathInfo = pathinfo($currentPath);
// output: localhost
$hostName = $_SERVER['HTTP_HOST'];
// output: http://
$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';
// return: http://localhost/myproject/
$full_url = $protocol.$hostName.$pathInfo['dirname']."/";
$home_url = (explode("/",$full_url));
return $home_url[0]."//".$home_url[2]."/".$home_url[3];
}
可能有一些bug,但现在我没有看到。如果你能指出一些,请告诉我。谢谢你
home\u url('data/cs\u menu.php')
它仍然返回“”,我想要的是“”,然后home\u url('cs\u menu.php')
?同样,我只想打印home url,它是“”,但它给了我“”,请参阅@ICGDEVS。但是在WordPress中,home\u url()
基于主页url的静态值-例如http://localhost/wp
(保存在数据库中,也可以在名为WP_HOME
的常量中设置或由其覆盖)。并且该函数接受一个可选参数-$path
。如果指定了$path
,您将得到'http://localhost/wp' . $路径
。否则,您总是会得到http://localhost/wp
-基本URL。请小心,因为代码可能不会在所有情况下或任何时候都有效-如果主URL始终采用此格式:{protocol}://{domain}/{sub folder in root}
(例如http://localhost/veco
),代码将起作用;否则,它将失败。就我的1美分。:)无论如何,别忘了接受你的答案——这对别人会有帮助的好的@SallyCJ。非常感谢你
$home_url = (explode("/",$full_url));
return $home_url[0]."//".$home_url[2]."/".$home_url[3];
function home_url()
{
// output: /myproject/index.php
$currentPath = $_SERVER['PHP_SELF'];
// output: Array ( [dirname] => /myproject [basename] => index.php [extension] => php [filename] => index )
$pathInfo = pathinfo($currentPath);
// output: localhost
$hostName = $_SERVER['HTTP_HOST'];
// output: http://
$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';
// return: http://localhost/myproject/
$full_url = $protocol.$hostName.$pathInfo['dirname']."/";
$home_url = (explode("/",$full_url));
return $home_url[0]."//".$home_url[2]."/".$home_url[3];
}