0

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?

2
  • @RiggsFolly Nope, I'm trying to determine which server the script is running on when it's executed through the command line. Commented Oct 7, 2014 at 15:45
  • That will teach me to read the question properly. Retracted! Commented Oct 7, 2014 at 15:47

3 Answers 3

1

You could simply look at the server's hostname:

<?php

$hostname = php_uname('n');
Sign up to request clarification or add additional context in comments.

2 Comments

Great idea, this works on both Windows & Linux. I did a little searching and found an even more concise command: php_uname('n');. I don't think this will different servers with the same hostname (such as if load balancing is used?), but for my purposes I think it will work fine.
That is the better way indeed, I've updated my answer.
1

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.

1 Comment

You're right, $_SERVER does appear to be set on both, but it's odd because the variables are different when viewed on the CLI (on linux some values are missing, while on windows others are missing). I don't feel like the data in it can be trusted since it seems to change based on the OS and who knows what else.
0

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 
    }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.