Codeigniter echoing [:: 1] instead of localhost

I am using CodeIgniter 3 as a web platform and am trying to import CSS semantic UI into my page. I do this using the CodeIgniter base_url() method in the href property to import CSS.

However, semantic.css itself imports some other fonts that are present on my server, which then cannot be downloaded due to Cross-Origin's resource sharing policy. This chrome error message gives me:

The font from the source ' http://[::1] ' was blocked when loading the Cross-Origin resource sharing policy: the header "Access-Control-Allow-Origin" is present on the requested resource. The origin of http: // localhost 'is therefore not allowed.

This is due to the fact that base_url () has something in common with the domain [::1] , and not with localhost, as I typed in the browser.

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

I made other projects very similar to this, and this "[::1]" never appeared. What exactly causes php to echo this way?

+7
php codeigniter codeigniter-2 codeigniter-3
source share
5 answers

Its because of your base_url empty.

In config / config.php

 $config['base_url'] = 'http://localhost/project_name'; 
+21
source share

To use base_url (); you must first download the url. This can be done either in application / config / autoload.php (on line 67 or near it): or you can manually use

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

than install

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

I think this will help you.

+3
source share

More accurate and dynamic way

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

Although you can still use the port.

+3
source share

You need to edit $ config ['base_url'] as shown below,

 $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 the code above to get a dynamic URL.

+3
source share

This is what you need to change in config / config.php, it works correctly in "localhost" as well as on 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'; } 
+1
source share

All Articles