简体   繁体   English

如何在本地主机上获取主页网址?

[英]How to get the home url on the localhost?

So I am making a website on my localhost, the folder name is "veco" therefore my URL link is http://localhost/veco/所以我在我的本地主机上创建一个网站,文件夹名称是“veco”,因此我的 URL 链接是http://localhost/veco/

Im currently using this code to get the home url " http://localhost/veco/ ":我目前使用此代码来获取主页网址“ http://localhost/veco/ ”:

<?php 
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/
    return $protocol.$hostName.$pathInfo['dirname']."/";
}
?>

Now I'm on page "about", my URL now is http://localhost/veco/about Now on the about page, I made a form现在我在“关于”页面上,我的 URL 现在是http://localhost/veco/about现在在关于页面上,我做了一个表格

<form action="data/cs_menu.php" method="POST">
    <input type="text" name="f_menu">
    <input type="submit" name="save_menu">
</form>

Now when I submit this form, it will redirect me to http://localhost/veco/about/data/cs_menu.php which is correct, but when I use the function home_url, it gives me " http://localhost/veco/data/ ", not " http://localhost/veco/ "现在,当我提交此表单时,它会将我重定向到http://localhost/veco/about/data/cs_menu.php ,这是正确的,但是当我使用 home_url 函数时,它给了我“ http://localhost/veco /data/ ",而不是 " http://localhost/veco/ "

Any ideas?有任何想法吗? I am not using Wordpress right now, but If you are familiar with WordPress, they have a function "home_url();"我现在没有使用 Wordpress,但是如果您熟悉 WordPress,它们有一个函数“home_url();” which returns the " http://localhost/FILENAME/ " which is I am trying to copy.它返回“ http://localhost/FILENAME/ ”,这是我试图复制的。

I am not using Wordpress right now, but If you are familiar to wordpress, they have function "home_url();"我现在没有使用 Wordpress,但是如果您熟悉 wordpress,它们有函数“home_url();”

WordPress looks up the value from a database, where it is set when you install WordPress. WordPress 从数据库中查找该值,该值是在您安装 WordPress 时设置的。

There is, generally, no way to determine the URL of the homepage from an arbitrary page on the site.通常,无法从站点上的任意页面确定主页的 URL。 The closest you could get would be to manually add a rule to every page along the lines of "The homepage is exactly two directories above this page".您可以获得的最接近的方法是沿着“主页正好是此页面上方的两个目录”的行手动向每个页面添加规则。

You can try this :你可以试试这个:

<?php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>

And, if you plan using https, you can use this function :而且,如果您计划使用 https,则可以使用此功能:

function base_url(){
  return sprintf(
  "%s://%s%s",
  isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
  $_SERVER['SERVER_NAME'],
  $_SERVER['REQUEST_URI']
 );
}

And call it this way :并这样称呼它:

echo url();
#=> http://127.0.0.1/foo

As for the $_SERVER global variable the docs can be found here至于 $_SERVER 全局变量,可以在此处找到文档

The original answer has been posted on this thread .原始答案已发布在此线程上

You should check it and let me know if this solves your problem.你应该检查一下,让我知道这是否能解决你的问题。 Hope this helps.希望这可以帮助。

Guys I finally found the solution, it is also dynamic.伙计们,我终于找到了解决方案,它也是动态的。 I only edited my function and added this:我只编辑了我的函数并添加了这个:

   $home_url = (explode("/",$full_url));
   return $home_url[0]."//".$home_url[2]."/".$home_url[3];

Now Here's the full final code:现在这是完整的最终代码:

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];

}

There might be some bugs to it, but for now I didn't see any.它可能有一些错误,但现在我没有看到任何错误。 If you can point some then please tell me.如果你能指出一些,那么请告诉我。 Thank you谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM