Search code examples
phpcodeignitercommand-lineenvironment-variablesserver-variables

Accessing $_SERVER variables from command line


How do I access $_SERVER variables I've set from the command line in PHP?

When I try to call a PHP method I've created I get the following error, which shows that all the $_SERVER variables are only defined when one calls my app via its URLs, i.e., webserver:

ERROR - Undefined index: MY_VAR /www/html/some_file.php
ERROR - Undefined index: MY_OTHER_VAR /www/html/some_file.php

To be more specific I'm using Codeigniter, but don't believe that is the issue.

Thoughts?


Solution

  • If you're setting the server variables in your web server config, then they won't be present when you access PHP via the command line. (Since the web server won't be involved at all.)

    To use $_SERVER variables in your CLI PHP script, see: Set $_SERVER variable when calling PHP from command line?


    To summarize:

    run: VALUE_ONE=1 ANOTHER_VALUE=2 php cli.php