簡單的方法來做到這一點......
定義在constants.php環境常數文件
// constants.php
define('ENVIRONMENT', 'development');
//define('ENVIRONMENT', 'production'); // uncomment this when your going to live your project
general.php定義一般函數
// general.php
include "constants.php";
function is_production()
{
if(ENVIRONMENT == "production")
{
return TRUE;
}
return FALSE;
}
function is_development()
{
if(ENVIRONMENT == "development")
{
return TRUE;
}
return FALSE;
}
現在你可以我們,在你的數據庫連接文件的功能,並選擇您的數據庫和基礎URL
// in db.php
include "general.php";
if(is_production())
{
$conn = mysql_connect("host1","username1","password1");
mysql_select_db("db1",$conn);
define('BASE_URL', 'http://domain.com');
}
else if(is_development())
{
$conn = mysql_connect("host2","username2","password2");
mysql_select_db("db1",$conn);
define('BASE_URL', 'http://testing.domain.com');
}
現在你可以使用BASE_URL常數,你有數據庫連接,只要你想 這是一般的概述,但你可以在你的項目中執行,因爲你的standered .. :)
謝謝,使用$ _SERVER ['SERVER_NAME']拉服務器url,然後通過一個IF語句傳遞完全是我想做的,現在救了我很多關於什麼時候gitignore等等 – Railto