php 在 CodeIgniter 中设置动态基本 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18075257/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:56:28  来源:igfitidea点击:

Set Dynamic Base Url in CodeIgniter

phpcodeigniter

提问by Janith Chinthana

I need to set dynamic base url in codeigniter due to few following reasons.

由于以下几个原因,我需要在 codeigniter 中设置动态基本 url。

  • localhost for development and reset when it goes live
  • single application accessible from multiple domains
  • ip address may change due to dhcp (locally)
  • 本地主机用于开发并在上线时重置
  • 可从多个域访问的单个应用程序
  • ip 地址可能会因 dhcp(本地)而改变

回答by Janith Chinthana

I just need to share my knowledge, since I already found the answeras mention below.

我只需要分享我的知识,因为我已经找到了下面提到的答案

Just overwrite the line in config/config.php with the following:

只需使用以下内容覆盖 config/config.php 中的行:

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

If you are using the sub folder you can use following code:

如果您使用的是子文件夹,则可以使用以下代码:

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

回答by Mudshark

CodeIgniter will figure out the base_urlby itself, so you can just do:

CodeIgniter 会base_url自己找出来,所以你可以这样做:

$config['base_url'] = '';

回答by Erich García

Just like say in the docs:

就像在文档中所说:

In case you need to allow e.g. multiple domains, or both http:// and https:// prefixes to be dynamically used depending on the request, remember that application/config/config.php is still a PHP script, in which you can create this logic with a few lines of code. For example:

如果您需要允许例如多个域,或者根据请求动态使用 http:// 和 https:// 前缀,请记住 application/config/config.php 仍然是一个 PHP 脚本,您可以在其中用几行代码创建这个逻辑。例如:

$allowed_domains = array('domain1.tld', 'domain2.tld');
$default_domain  = 'domain1.tld';

if (in_array($_SERVER['HTTP_HOST'], $allowed_domains, TRUE))
{
    $domain = $_SERVER['HTTP_HOST'];
}
else
{
    $domain = $default_domain;
}

if (! empty($_SERVER['HTTPS']))
{
    $config['base_url'] = 'https://'.$domain;
}
else
{
    $config['base_url'] = 'http://'.$domain;
}

回答by Rakhi Prajapati

Also you can try this.

你也可以试试这个。

$base_url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$base_url .= "://". @$_SERVER['HTTP_HOST'];
$base_url .=     str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $base_url;

回答by Terry Lin

I agree with Janith's answer but I have a better and easier solution.

我同意 Janith 的回答,但我有一个更好、更简单的解决方案。

Set the config item in any config files (for example, I use it in routes.php), it will reset base_url before using in Controller.

在任何配置文件中设置配置项(例如,我在routes.php中使用它),它会在Controller中使用之前重置base_url。

$lang_code = 'en';
$root_domain = 'test.org';
$base_domain_url = 'http://' . $lang_code . '.' . $root_domain;

$this->config->set_item('base_url', $base_domain_url);

Test it in Controller:

在控制器中测试:

echo base_url();

Result:

结果:

http://en.test.org

回答by Hafizur Rahman

Also you can use...

你也可以使用...

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

回答by Sajan Saj

This will work for command line, localhost and production.

这将适用于命令行、本地主机和生产。

$protocol = is_https() ? "https://" : "http://";
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : "";
if(is_cli())
{
    $config['base_url'] = '';
}
elseif(stristr($host, "localhost") !== FALSE || (stristr($host, '192.168.') !== FALSE) || (stristr($host, '127.0.0') !== FALSE))
{
    $config['base_url'] = $protocol.$host."/PROJECT_FOLDER/";
}
else
{
    $allowed_hosts = ['sajansaj.com', 'www.sajansaj.com'];
    $config['base_url'] = in_array($host, $allowed_hosts) ? $protocol.$host."/" : "we-do-not-recognise-this-host.com";
}

回答by Marcos Rufino

in my case I was presenting the error of mixed contentfor https using proxy, I used https and everything worked out. Just overwrite the line in config/config.php with the following:

就我而言,我使用代理呈现了https的混合内容错误,我使用了 https,一切都解决了。只需使用以下内容覆盖 config/config.php 中的行:

$config['base_url']    = 'https://'.$_SERVER['HTTP_HOST'].'/';
$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url']    = "$root";