How To Detect In Console App Whether My App Is Live Or Local

I have different db connections for local and live app as below:




if(isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == 'localhost')

{

    $dbhost = 'localhost';

    $dbname = 'dbName';

    $dbuser = 'root';

    $dbpass = '';

}

else

{

    $dbhost = 'localhost';

    $dbname = 'dbName';

    $dbuser = 'root';

    $dbpass = 'myLivePassworD';

}

This works great! The problem I’m facing now is to use the same thing on my console application, but there I don’t have any $_SERVER[‘HTTP_HOST’]. How to check in console.php file that I should connect to a live or local db?

Here is what I do:





/**

 * Common configuration file.

 */


/**

 * CliCheck

 *

 * Helper class to check if we are on the command line.

 */

class CliCheck

{

    /**

 	* Advanced PHP-CLI mode check.

 	*

 	* @return boolean    Returns true if PHP is running from the CLI or else false.

 	*

 	* @access public

 	* @static

 	*/

    public static function isCli()

    {

        // If STDIN was not defined and PHP is running as CGI module

        // we can test for the environment variable TERM. This

        // should be a right way how to test the circumstance under

        // what mode PHP is running.

        if(!defined('STDIN') && self::isCgi()) {

            // STDIN was not defined, but if the environment variable TERM

            // is set, it is save to say that PHP is running from CLI.

            if(getenv('TERM')) {

                return true;

            }

            // Now return false, because TERM was not set.

            return false;

        }

        return defined('STDIN');

    }


    /**

 	* Simple PHP-CGI mode check.

 	*

 	* (DSO = Dynamic Shared Object)

 	*

 	* @link http://httpd.apache.org/docs/current/dso.html DSO

 	* @link http://www.php.net/manual/en/function.php-sapi-name.php PHP_SAPI

 	*

 	* @return boolean    Returns true if PHP is running as CGI module or else false.

 	*

 	* @access public

 	* @static

 	*/

    public static function isCgi()

    {

        if (substr(PHP_SAPI, 0, 3) == 'cgi') {

            return true;

        } else {

            return false;

        }

        return false;

    }

}


if(!CliCheck::isCli()) {

     //$host = $_SERVER['HTTP_HOST']; // Alternative, switched to SERVER_NAME myself.


    $host = getenv('SERVER_NAME');

} else {

    $host=gethostname();

}

/** Check if there is a site configuration */

$siteconf = dirname(__FILE__).DIRECTORY_SEPARATOR.$host.'_common.php';

if(file_exists($siteconf)) {

    $siteconf=require($siteconf);

} else {

    print "No site configuration $siteconf";

    $siteconf=array();

}




global $isProduction;

if(!isset($isProduction)) {

    $isProduction=false;

}

I set my database information in the ‘…_common.php’ or elsewhere. You can do


'db'=>$isProduction?array(firstconfig):array(secondconfig);

.

Personally, I do:





$confarray=CMap::mergearray($confarray,$siteconf);

return $confarray;



Where $confarray is a common configuration, and $siteconf is loaded from the ‘<site>_common.php’.

There are some further tricks to make sure that CMap is available, but this is beyond the scope of your question which is about checking the hostname.

gethostname() function solved my problem and of course saved my day. :wink:

Thanks.