Nate
Nate

Reputation: 28444

How to differentiate servers in PHP script executed via shell?

I have a PHP script that is executed using shell_exec(). In the script, I need to check whether the script is running on my development machine (using WampServer on Windows) or my production server (using Linux) because the database credentials are different and the script needs to know which set of credentials to use.

Because the script is executed in the shell, the $_SERVER variables are not set and I can't identify which machine the script is running on with this method (which is how I do it in other scripts).

How can I determine which server the script is running on when it is executed via shell?

Upvotes: 0

Views: 69

Answers (3)

naneau
naneau

Reputation: 489

You could simply look at the server's hostname:

<?php

$hostname = php_uname('n');

Upvotes: 1

RiggsFolly
RiggsFolly

Reputation: 94682

I have done this in a number of ways, but assuming you have control of the development machine you could check for an environment variable that wont exist on your LIVE server, or create one. For example :-

Add an environment variable to windows called IAMDEV=1, then

<?php
    if ( get_env('IAMDEV') === FALSE ) {
        // we are LIVE
    } else {
        // we are DEV 
    }
?>

Upvotes: 0

blue112
blue112

Reputation: 56572

Running PHP 5.3 on a Linux box, $_SERVER is defined in CLI.

Try to run :

php -r "print_r(\$_SERVER);"

If it's not defined in a Windows box, then it's a simple way to differentiate them too.

Upvotes: 1

Related Questions