what is the difference between site_url() and base_url()?

echo base_url(); // http://example.com/website
echo site_url(); // http://example.com/website/index.php

if you want a URL access to a resource (such as css, js, image), use base_url(), otherwise, site_url() is better.

for a detailed reference Check this both function in CodeIgniter.

public function site_url($uri = '')
    {
        if (empty($uri))
        {
            return $this->slash_item('base_url').$this->item('index_page');
        }
        $uri = $this->_uri_string($uri);
        if ($this->item('enable_query_strings') === FALSE)
        {
            $suffix = isset($this->config['url_suffix']) ? $this->config['url_suffix'] : '';
            if ($suffix !== '')
            {
                if (($offset = strpos($uri, '?')) !== FALSE)
                {
                    $uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset);
                }
                else
                {
                    $uri .= $suffix;
                }
            }
            return $this->slash_item('base_url').$this->slash_item('index_page').$uri;
        }
        elseif (strpos($uri, '?') === FALSE)
        {
            $uri = '?'.$uri;
        }
        return $this->slash_item('base_url').$this->item('index_page').$uri;
    }

Base URL function.

public function base_url($uri = '')
        {
            return $this->slash_item('base_url').ltrim($this->_uri_string($uri), "https://stackoverflow.com/");
        }

Leave a Comment