Codeigniter echoing [::1] instead of localhost

后端 未结 5 1599
误落风尘
误落风尘 2020-12-03 05:38

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 hre

相关标签:
5条回答
  • 2020-12-03 06:12

    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';
    
    }
    
    0 讨论(0)
  • 2020-12-03 06:17

    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.

    0 讨论(0)
  • 2020-12-03 06:25

    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.

    0 讨论(0)
  • 2020-12-03 06:29

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

    0 讨论(0)
  • 2020-12-03 06:29

    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

    0 讨论(0)
提交回复
热议问题