php Codeigniter 回显 [::1] 而不是 localhost
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35876187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter echoing [::1] instead of localhost
提问by Dalannar
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.
我使用 CodeIgniter 3 作为网络平台并尝试将语义 UI CSS 导入我的页面。我是通过base_url()
在 CSS 导入的 href 属性中使用 CodeIgniter 的方法来实现的。
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:
但是,semantic.css 本身会导入我服务器上存在的一些其他字体,由于跨域资源共享策略,这些字体随后无法加载。这是 chrome 给我的错误消息:
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.
来自源“ http://[::1]
”的字体已被跨源资源共享策略阻止加载:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问Origin ' http://localhost'。
This is because base_url() echoes the domain has being [::1]
and not localhost as I've typed into the browser.
这是因为 base_url() 回显了域,[::1]
而不是我在浏览器中输入的 localhost。
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]
出于某种原因,在我看来,chrome(以及 Edge)并未将[::1]
localhost 视为同一主机,或者我可能只是愚蠢。我所知道的是,如果我更改主语义.css 文件的路径并将 localhost 硬编码到其中,它就可以工作,并且如果我使用 localhost 而不是请求我的页面,它也可以工作[::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 ?
我做过与此非常相似的其他项目,但从未"[::1]"
出现过。究竟是什么导致 php 回显这样的路径?
回答by Abdulla Nilam
It's because of your base_url
is empty.
那是因为你base_url
是空的。
In config/config.php
在config/config.php
$config['base_url'] = 'http://localhost/project_name';
Something more interesting about
http://\[::1\]/
更有趣的事情
http://\[::1\]/
回答by Rakib
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.
虽然你仍然可以使用端口。
回答by Sugan Krishna
You need to edit your $config['base_url']as follows,
你需要编辑你的$config['base_url']如下,
$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.
文件位置:codeigniter/application/config/config.php
使用以上代码获取动态url。
回答by Muhammad Talha
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
为了使用 base_url(); 您必须首先加载 URL Helper。这可以在 application/config/autoload.php(第 67 行或附近)中完成:或者您可以手动使用
$this->load->helper('url');
than set the
比设置
$config['base_url'] = 'http://localhost/your_site_url';
i think it will help you
我想它会帮助你
回答by Kartik Bhat
This is what you need to alter in config/config.php, it works properly in "localhost" as well as in your "server":
这是您需要在 config/config.php 中更改的内容,它可以在“localhost”和“服务器”中正常工作:
$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';
}