Code Igniter基于http_host选择数据库

时间:2011-05-14 19:28:26

标签: php codeigniter

我有CodeIgnitor设置来根据加载的子域选择数据库(如下所示)。

当我使用临时表和不同的数据库时,我转向了持久连接。它似乎工作正常,但我只是想知道是否有任何潜在的问题。 (我也使用数据库支持的会话)

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$active_group = "default";
$active_record = TRUE;
$phppos_client_name = substr($_SERVER['HTTP_HOST'], 0, strpos($_SERVER['HTTP_HOST'], '.'));
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "USER";
$db['default']['password'] = "PASSWORD";
$db['default']['database'] = 'db_'.$phppos_client_name;
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "phppos_";
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";


/* End of file database.php */
/* Location: ./application/config/database.php */

1 个答案:

答案 0 :(得分:0)

它可以工作,但是因为用户可以输入HTTP_HOST的任何值,因为它将从HTTP请求头中获取。因此,您应该使用可用的数据库检查HTTP_HOST的值。由于用户输入的错误值(拼写错误的子域名)将导致数据库错误。

或者您可以使用SERVER_NAME,但请在切换前查看HTTP_HOST vs. SERVER_NAME讨论。