php codeigniter:为什么当我在锚标记的 href 属性中回显 base_url() 时,它会回显两次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11444121/
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 00:32:08  来源:igfitidea点击:

codeigniter: why is that when i echo base_url() in an href attribute of an anchor tag, it echoes twice

phpcodeigniterurlanchorbase-url

提问by dave

so basically when i echo the codeigniter function base_url() in the href attribute of an anchor tag, it appears to echo it out twice. Example:

所以基本上当我在锚标记的 href 属性中回显 codeigniter 函数 base_url() 时,它似乎会回显两次。例子:

<a href="<?php echo base_url(); ?>">somelink</a>

and the above, if you inspect it your chrome browser shows this:

以上,如果你检查它,你的 chrome 浏览器会显示这个:

<a href="www.mysitedomainname.com/www.mysitedomainname.com/">somelink</a>

"mysitedomainname.com" is just a name i made up for this example. Any reason why this is happening?

“mysitedomainname.com”只是我为这个例子编的一个名字。发生这种情况的任何原因?

回答by Sergey Telshevsky

There are three reasons I'm aware about that can cause this.

我知道有三个原因可能会导致这种情况。

The first one is when something wrong is written in config.phpon line 17 $config['base_url'] = '';- it better be left empty, just like when you download CI.

第一个是当config.php第 17 行写入错误时$config['base_url'] = '';- 最好将其留空,就像您下载 CI 时一样。

The second one is if you have set $config['base_url']value to something without prefixing it with http://or other protocol.

第二个是如果您已将$config['base_url']value设置为某些内容而没有为其添加前缀http://或其他协议。

The third one is if you have set basehrefsomewhere:

第三个是如果你在basehref某处设置:

<base href="http://www.mysitedomainname.com/" />

When you need to link to some other page, you shoulduse site_url(), base_url()can be used to link stylesheets, js, img src attributes and other realURL's. The reason is pretty simple, base_url()does not include the index_pagevalue set in config.php.

当您需要链接到其他页面时,您应该使用site_url(),base_url()可用于链接样式表、js、img src 属性和其他真实URL。原因很简单,base_url()不包括中index_page设置的值config.php

回答by Gautam3164

It will work fine if you use

如果你使用它会正常工作

<a href="<?php echo base_url('Controller/Function'); ?>">somelink</a>

回答by Bryan Bojorque

try this

尝试这个

make this

做这个

$config['base_url'] = "http://www.mysitedomainname.com"

into this

进入这个

$config['base_url'] = ""

in config.php

在 config.php