Last active
August 29, 2015 14:20
-
-
Save yeondudad/b3941b7c021822b834ac to your computer and use it in GitHub Desktop.
Changeable configuration in php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
global $conf; | |
if (!isset($conf)) { | |
$pathTemplate = dirname(__FILE__) . '/conf_%s.php'; | |
$configFile = sprintf($pathTemplate, 'script'); | |
$currentConfigFile = sprintf($pathTemplate, gethostname()); | |
if (file_exists($currentConfigFile)) { | |
$configFile = $currentConfigFile; | |
} elseif (array_key_exists('HTTP_HOST', $_SERVER)) { | |
$currentConfigFile = sprintf($pathTemplate, $_SERVER['HTTP_HOST']); | |
if (file_exists($currentConfigFile)) { | |
$configFile = $currentConfigFile; | |
} else { | |
$configFile = sprintf($pathTemplate, 'localhost'); | |
} | |
} | |
$conf = require $configFile; | |
} | |
return $conf; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return array( | |
"conf_key" => "this is localhost." | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return array( | |
"conf_key" => "this is script." | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return array( | |
"conf_key" => "this is test.com." | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$_SERVER['HTTP_HOST'] = 'localhost'; | |
$conf = require 'conf.php'; | |
echo $conf["conf_key"]; // this is localhost. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$conf = require 'conf.php'; | |
echo $conf["conf_key"]; // this is script. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$_SERVER['HTTP_HOST'] = 'test.com'; | |
$conf = require 'conf.php'; | |
echo $conf["conf_key"]; // this is test.com. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment