![]() |
|
Snippets |
|
With this snippet we can implement the following functionalities from the global configuration file config/config.php:
Access control to the applications in certain environments. For example, disable access in the environment of development from production server.
IP access control to certain applications of the project. For example, to protect backend or administration zones of the project.
To have several global configuration files of config/config.php according to the environment in which we are. For example, to define different constants for each environment.
I developed a mini-library (config/config.lib.php) to include and use in the global config file config/config.php.
<?php /** * IP access control to an environment * * @param string $env environment name * @param array $ips IPs that has access * @param string $clientip client IP */ function envCorrecte ($env,$ips,$clientip) { if (SF_ENVIRONMENT==$env) { $acces=0; foreach($ips as $ip) { $ipclient=substr($clientip,0,strlen($ip)); if ($ipclient==$ip) { $acces=1; } } if ($acces==0) { echo "Keep away! this is not a public environment."; exit; } } } /** * IP access control to an application of the symfony project * * @param array $apps no public applications * @param array $ips IPs that has access * @param string $clientip client IP */ function appCorrecte ($apps,$ips,$clientip) { // solve problem with key '0' in array $apps_aux[0]=''; $apps = $apps_aux + $apps; // if (array_search(SF_APP,$apps)!=false) { $acces=0; foreach($ips as $ip) { $ADR=$HTTP_SERVER_VARS['REMOTE_ADDR']; $ipclient=substr($clientip,0,strlen($ip)); if ($ipclient==$ip) { $acces=1; } } if ($acces==0) { echo "Keep away! ".SF_APP." is not a public application."; exit; } } } ?>
Finally config/config.php file can be like this:
<?php include("config.lib.php"); /** * IP access control to 'env' and 'int' environments */ $ip_dev=array("10.138.0.","192.168."); $ip_int=array("10.138.0.","192.168."); envCorrecte('env',$ip_env,$HTTP_SERVER_VARS['REMOTE_ADDR']); envCorrecte('int',$ip_int,$HTTP_SERVER_VARS['REMOTE_ADDR']); /** * IP access control to backend applications */ $ip_apps=array("10.138.0.","192.168.","w.x.y.z"); $apps=array('myapp_1','myapp_2','myapp_n'); appCorrecte($apps,$ip_apps,$HTTP_SERVER_VARS['REMOTE_ADDR']); /** * Custom config.php file for each environment */ include("config.".SF_ENVIRONMENT.".php"); /** * Common config.php file for all environments */ define("example","content1"); ?>