Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 687
dave Avatar asked Jul 12 '12 02:07

dave


People also ask

What is the difference between Site_url and base_url?

base_url is without the index_page or url_suffix being appended. like site_url , you can supply segments as a string or an array. If you want a URL access to a resource use base_url you can supply a string to a file, such as an image or stylesheet else site_url is enough.

What is base_url () in php?

base_url() is the URL of your CodeIgniter root or the location of the index. php file along with a trailing slash appended to the end of the URL. If the domain name is example.com and the website is using HTTPS as well as www. Further, the index.

What is the use of base URL in CodeIgniter?

The base URL of the site can be configured in application/config/config. php file. It is URL to your CodeIgniter root.

How do I link to another page in CodeIgniter?

site_url() : Returns your site URL, as specified in your config file. The index. php file (or whatever you have set as your site index_page in your config file) will be added to the URL, as will any URI segments you pass to the function, and the url_suffix as set in your config file.


1 Answers

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.

like image 78
Sergey Telshevsky Avatar answered Sep 30 '22 08:09

Sergey Telshevsky