php更改数据库与子域

时间:2017-12-20 17:20:40

标签: php mysql

我搜索了谷歌,stackoverflow,但所有与php框架相关的主题都不是核心php,我想知道根据子域更改数据库的最佳解决方案是什么。我有一个多语言网站,我想使用单一源代码,但有不同的子域名,只需根据子域名更改数据库。

这是我的代码:

$domain = $_SERVER['HTTP_HOST'];
$sub = array_shift((explode('.', $domain)));
if ($sub == 'tr') {
  $servername = "localhost";
  $username = "db_tr";
  $password = "xxxxx";
  $dbname = "xxxx";
} else if ($sub == 'gb') {
  $servername = "localhost";
  $username = "db_gb";
  $password = "xxxxx";
  $dbname = "xxxx";
} else if ($sub == 'us') {
  $servername = "localhost";
  $username = "db_us";
  $password = "xxxxx";
  $dbname = "xxxx";
} else if ($sub == 'ca') {
  $servername = "localhost";
  $username = "db_ca";
  $password = "xxxxx";
  $dbname = "xxxx";
} else {
  $servername = "localhost";
  $username = "db_all";
  $password = "xxxxx";
  $dbname = "xxxx";
}

所以我有两个问题:

  1. 这种逻辑在编程方面是否合适?如果没有,这样做的最佳解决方案是什么?
  2. 有害吗?例如sql注入,黑客攻击或其他安全问题。

0 个答案:

没有答案