Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 939
tim peterson Avatar asked Dec 21 '25 01:12

tim peterson


1 Answers

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

like image 86
Joel L Avatar answered Dec 22 '25 15:12

Joel L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!