15

I am using CodeIgniter 3 as a web platform and trying to import semantic-UI CSS into my page. I'm doing so by using CodeIgniter's base_url() method in the href property for the CSS import.

However, semantic.css itself imports some other fonts on my server, which cannot load because of Cross-Origin resource sharing policy. This is the error message chrome gives me:

Font from origin 'http://[::1]' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

This is because base_url() echoes the domain has been [::1] and not localhost as I've typed into the browser.

For some reason, it appears to me that chrome (and also Edge) does not consider [::1] and localhost as the same host, or maybe I'm just being dumb. What I know though is that if I change the path of the main semantic.css file and complex code localhost into it, it works, and it also works if, instead of requesting my page using localhost, I use [::1]

I've done other projects very similar to this and never had this "[::1]" appear. What exactly is causing PHP to echo such a path?

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
Dalannar
  • 381
  • 1
  • 2
  • 11

5 Answers5

40

It's because of your base_url is empty.

In config/config.php

$config['base_url'] = 'http://localhost/project_name';

Something more interesting about http://\[::1\]/

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • Well I realize this, but I've never filled it in and the base_url has always been correctly resolved whether it was on localhost or some other domain. If I filled this value in I would imagine I would need to change it when moving my project from my machine to some other server right ? – Dalannar Mar 08 '16 at 19:46
  • Some other server means?? – Abdulla Nilam Mar 08 '16 at 19:46
  • If I moved to say example.com, would base_url still echo localhost had I set that ? – Dalannar Mar 08 '16 at 20:00
  • When you hosted it to live server, (ex: http://stackoverflow.com/) so you have to change baseurl as well – Abdulla Nilam Mar 08 '16 at 20:01
  • 1
    That's what I'm trying to avoid, since It'll be quite often that I change this project from my machine to a live server I wanted to have all of this change automatically so both versions can be the same. – Dalannar Mar 08 '16 at 20:53
9

You need to edit your $config['base_url'] as follows,

$config['base_url'] = '';
$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']);

File location: codeigniter/application/config/config.php
Use above code to get dynamic url.

Sugan Krishna
  • 413
  • 4
  • 11
7

More accurate and dynamic way

$root = "http://".$_SERVER['HTTP_HOST'];
$root .= dirname($_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;

Though you can still use port.

Rakib
  • 98
  • 1
  • 5
4

In order to use base_url(); you must first have the URL Helper loaded. This can be done either in application/config/autoload.php (on or around line 67): or you can manually using

$this->load->helper('url');

than set the

$config['base_url'] = 'http://localhost/your_site_url';

i think it will help you

Muhammad Talha
  • 337
  • 5
  • 19
1

This is what you need to alter in config/config.php, it works properly in "localhost" as well as in your "server":

$config['base_url'] = "http://".$_SERVER['SERVER_NAME'];

$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

if(!defined('DOCUMENT_ROOT')) define('DOCUMENT_ROOT',str_replace('application/config','',substr(__FILE__, 0, strrpos(__FILE__, '/'))));

$config['base_path'] = constant("DOCUMENT_ROOT");

$config['js_url'] = $config['base_url'].'js/';

$config['css_url'] = $config['base_url'].'css/';

$config['image_url'] = $config['base_url'].'img/';

// Host resolution for cross origin requests

if(ENVIRONMENT == 'production') {
    $config['host'] = 'www.<domain_name>.com';
} else {

$config['host'] = 'localhost';

}
Leopold Joy
  • 4,524
  • 4
  • 28
  • 37