PHP $_SERVER['SERVER_NAME'] 正确使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5128474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:53:33  来源:igfitidea点击:

PHP $_SERVER['SERVER_NAME'] correct use

php

提问by Kleber Stender

noob question here

菜鸟问题在这里

I'm building a interactive web site using php, and i decided to do one interative menu. Well, my menu consists in one file called menu.php.

我正在使用 php 构建一个交互式网站,我决定做一个交互式菜单。嗯,我的菜单包含在一个名为 menu.php 的文件中。

<?php
    echo "<ul>
        <li><a href='".$_SERVER['SERVER_NAME']."/menu/register.php'>Cadastro</a><br></li>
        </ul>";
?>

I don't know why, but the when i click on this link it appears like that

我不知道为什么,但是当我单击此链接时,它看起来像那样

htpp://localhost/my_site/localhost/menu/register.php

htpp://localhost/my_site/ localhost/menu/register.php

EDIT: How can i delete that additional localhost?

编辑:如何删除额外的本地主机?

Thanks in advance

提前致谢

回答by Pekka

That is because you are not using the http://prefix. You would have to use

那是因为您没有使用http://前缀。你将不得不使用

   <li><a href='http://".$_SERVER['SERVER_NAME']."/menu/register.php'></li>

(assuming http://is the only protocol your site can be on)

(假设http://是您的网站可以使用的唯一协议)

However, if you have a fixed site structure, consider using relative paths that work just as well, but with less headache:

但是,如果您有固定的站点结构,请考虑使用同样有效的相对路径,但不会让您头疼:

 <li><a href='/menu/register.php'></li>

回答by Gumbo

$_SERVER['SERVER_NAME']contains just the name of the web server/virtual host:

$_SERVER['SERVER_NAME']仅包含 Web 服务器/虚拟主机的名称:

'SERVER_NAME'
The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host.

'SERVER_NAME'
正在执行当前脚本的服务器主机的名称。如果脚本在虚拟主机上运行,​​这将是为该虚拟主机定义的值。

In your case it's localhost. And appending a path like /menu/register.phpto it results in the relative path:

在你的情况下,它是localhost. 并附加一个类似的路径/menu/register.php会导致相对路径:

localhost/menu/register.php

Now when you use that URI reference in a document that's base URI is http://localhost/my_site/(or a similar URI having the same path prefix), that relative path localhost/menu/register.phpgets resolved by the client to:

现在,当您在基本 URI 为http://localhost/my_site/(或具有相同路径前缀的类似 URI)的文档中使用该 URI 引用时,该相对路径localhost/menu/register.php由客户端解析为:

http://localhost/my_site/localhost/menu/register.php

That's the reason for the output you're getting.

这就是你得到输出的原因。

So what are you actually trying to accomplish?

那么你实际上想要完成什么?

回答by Quentin

Just use relative URIs.

只需使用相对 URI。

<li><a href='/menu/register.php'>Cadastro</a></li>

(You can ditch the <br>too. Use CSS padding if you want padding.)

(你也可以放弃<br>。如果你想要填充,请使用 CSS 填充。)

回答by azat

You must write like this:

你必须这样写:

echo "<ul>
    <li><a href='http://".$_SERVER['SERVER_NAME']."/menu/register.php'>Cadastro</a><br></li>
    </ul>";

Or simple

或者简单

echo "<ul>
    <li><a href='/menu/register.php'>Cadastro</a><br></li>
    </ul>";

回答by Orbit

You could just omit the

你可以省略

 $_SERVER['SERVER_NAME']

and use relative pathing.

并使用相对路径。

回答by Deep Kapadia

good practice is not to use the server name in a href's

好的做法是不要在 href 中使用服务器名称

<a href="/menu/register.php">register</a>

or

或者

<a href="menu/register.php">register</a>

should work just fine

应该工作得很好

if you really needed to use the server name you could use:

如果您确实需要使用服务器名称,则可以使用:

<a href='http://".$_SERVER['SERVER_NAME']."/menu/register.php'>Cadastro</a>

the additional localhost is because the browser interprets it as a string and not a URL

额外的 localhost 是因为浏览器将其解释为字符串而不是 URL

回答by Nick Donley

I would also recommend using $_SERVER['HTTP_HOST'] over $_SERVER['SERVER_NAME'] that Ankur Kumar Singh said in a comment.

我还建议使用 $_SERVER['HTTP_HOST'] 而不是 Ankur Kumar Singh 在评论中所说的 $_SERVER['SERVER_NAME'] 。

<li><a href='".$_SERVER['HTTP_HOST']."/menu/register.php'>Cadastro</a><br></li>

<li><a href='".$_SERVER['HTTP_HOST']."/menu/register.php'>Cadastro</a><br></li>

This worked for me and got rid of the local host issue on my sites.

这对我有用并摆脱了我网站上的本地主机问题。