Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter echoing [::1] instead of localhost

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 present on my server, which are then unable to 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 being [::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 hard 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 ?

like image 276
Dalannar Avatar asked Mar 08 '16 19:03

Dalannar


2 Answers

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\]/

like image 165
Abdulla Nilam Avatar answered Sep 23 '22 06:09

Abdulla Nilam


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.

like image 40
Sugan Krishna Avatar answered Sep 22 '22 06:09

Sugan Krishna