codeigniter

Base url in Codeigniter

Setting your base url in Codeigniter

You will need to set your base URL in application/config/config.php

If it is not set, then CodeIgniter will try to guess the protocol and path to your installation, but due to the security concerns the hostname will be set to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise. The auto-detection mechanism exists only for convenience during development and MUST NOT be used in production!

$config['base_url'] = '';

It should be filed like

$config['base_url'] = 'https://localhost/projectname/';

$config['base_url'] = 'https://www.example.com/';

Always good to use / at end of base_url

When you do not set your base URL you might run into some errors where you can not load your CSS, images, and other assets items. And also you might have trouble submitting forms as some users have come across.

Update

If you do not want to set your base URL another way is.

Create a new core file in application/core/MY_Config.php

And paste this code

<?php

class MY_Config extends CI_Config {

    public function __construct() {

        $this->config =& get_config();

        log_message('debug', "Config Class Initialized");

        // Set the base_url automatically if none was provided

        if ($this->config['base_url'] == '')
        {
            if (isset($_SERVER['HTTP_HOST']))
            {
                $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
                $base_url .= '://'. $_SERVER['HTTP_HOST'];
                $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
            }

            else
            {
                $base_url = 'https://localhost/';
            }

            $this->set_item('base_url', $base_url);

        }
    }
}

Something More About base_url

What happens if I don’t set base_url ?

You will not get any Impotency error to set this and proceed. You can continue without setting, but you should know about HTTP header injection


If I did’t set it what will show up?

You will get https://[::1]/ instead of your actual URL.


What does this mean https://[::1]/ ??

This is temporary URL which set by CI by Default. This will point the root of your document.

::1 - Server address (localhost) Read More about this


How to set proper base_url()??

Base URL should always point to root of your project folder. (outside application folder)

$config['base_url'] = 'https://localhost/path/to/project'; # If localhost
$config['base_url'] = 'https://stackoverflow.com/'; # If live
$config['base_url'] = 'https://stackoverflow.com/documentation'; # If live & inside subdomain (assume documentation is subfolder/subdomain)

How to use base_url()??

Most common use is to find the right path to your js or css files.

<link rel="stylesheet" href="<?php echo base_url('styles/style.css');?>" />
<script src="<?php echo base_url('vendor/jquery/jquery.min.js');?>"></script>

Adding the code above in your view will produce HTML as below:

<link rel="stylesheet" href="https://localhost/path/to/project/styles/style.css" />
<script src="https://localhost/path/to/project/vendor/jquery/jquery.min.js"></script>

Links

  1. URL Helper

Smart way to setting up the base_url

The following lines of code is more smart way to setting up the base_url in codeigniter:

$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

Recommended is

$config['base_url'] = 'https://stackoverflow.com/';

Because everyone knows the hosting space. So if you set like this you can prevent Injection to your site/host.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow