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

我的未来我决定 提交于 2019-12-10 13:44:39

问题


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:

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

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

<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?


回答1:


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

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

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

The third one is if you have set base href somewhere:

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

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




回答2:


try this

make this

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

into this

$config['base_url'] = ""

in config.php




回答3:


It will work fine if you use

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


来源:https://stackoverflow.com/questions/11444121/codeigniter-why-is-that-when-i-echo-base-url-in-an-href-attribute-of-an-ancho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!