The environment variable logonserver is available from the cmd prompt
echo %logonserver%
Is it possible to read this variable from php which will be accessed via a browser and not the cmd prompt, which means I can't pass the value in as a parameter.
I know I could shell out to cmd and get it that way, but I was wondering if there was some built-in php functionality for this.
Yes. All environment variables are accessible through super-global $_SERVER variable. Just try var_dump($_SERVER) to see what's inside.
Related
I'm looking for a way to export an environment variable from a PHP script running with PHP CLI. The putenv function only permit to set variable that will be existing during script execution, not after.
My use case is a PHP script used as bash autocompleter function. In this type of function, we have to set COMPREPLY variable that must contain the list of available options (as a bash array). Currently, I'm setting this variable by this way :
COMPREPLY=( $(php myscript.php) )
I would like to have possibility to set COMPREPLY variable and some others (especially for action on default behavior on empty options list).
Thank you in advance
I want to be able to access environment variables set in Powershell from within my PHP script.
Normally in Linux systems export SOMETHING=foo would work and in Windows CMD SET SOMETHING=bar would also work. But I don't know how to make this work with Powershell CLI (not script) as well.
I have tried using Set-Variable -Name "SOMETHING" -Value "foo", but that didn't get read by PHP.
Within my PHP script I simply want to use getenv('SOMETHING');.
There are several ways to create environment variables in PowerShell. It is important to distinguish between PowerShell variables (e.g. $myVar) and Environment variables as you might know them from the good old command prompt (e.g. in cmd SET MYVAR=MYVAL).
To create an environment variable in PowerShell, the easiest way is to create a new Item in the environment (Env):
New-Item Env:/VarName -Value "Foo"
The code example you posted
Set-Variable -Name "Name" -Value "value"
creates a PowerShell variable, but it's easier to just write $Name = "value".
PowerShell variables are only visible in your PowerShell scope. Environment variables can be read by any program you start as a sub process in the current environment (e.g. PowerShell session).
EDIT:
The example posted in the comments by #f64a is interesting too:
[environment]::SetEnvironmentVariable("myName", "myValue",[System.EnvironmentVariableTarget]::Machine)
It is afaik the only way to manipulate environment variables via PowerShell out of your own scope. With EnvironmentVariableTarget you can choose where your variable is manipulated. Be aware that you might need higher privileges to manipulate these.
I am working on a simple python web server for educational purposes. I want my server to be able to run PHP scripts on requests. I am able to execute php codes by creating a subprocess like in this answer and get its output as bytes. This can help me to execute the most of the PHP built-in functions. But I would like also to simulate the $_GET and $_POST variables, like, when the user send a POST request, I would like its parameters to be available in $_POST global variable.
I may hack the the command I give to the subprocess, but I heard that, in a real web server like Apache, PHP gets the value of these variables from the environment variables (Common Gateway Interface standard, I think).
However, when I set an environment variable called QUERY_STRING php doesn't set the value of $_GET. Let me show the big picture:
index.php
<?php var_dump($_GET); ?>
my commands in shell:
export QUERY_STRING="foo=bar"
php index.php
This command snipped doesn't give me the output I want (Array => ['foo'] = 'bar'). Here is my questions:
What is my mistake in the above code snipped ?
How can I inject these two global variables ($_GET and $_POST) to a PHP scripts in python?
My python version is 3.4.3 and I use ubuntu 14.04
Try this:
$ php -f index.php foo=bar
And in index php do:
parse_str(implode('&', array_slice($argv, 1)), $_GET);
After that your will get this rezult:
echo $_GET['foo'];// prints 'bar'
Read more in example in the end of the page
I need to run a PHP script from command line and I need to set some environmental variables. Unfortunately, following does not work:
php -dAPPLICATION_ENV=staging script.php
What I'd like to accomplish is having APPLICATION_ENV variable set.
APPLICATION_ENV=staging php script.php
The variable will be available in the $_SERVER array:
echo $_SERVER['APPLICATION_ENV'];
There is no way to set environment variables from the command line specifically for the execution of a script by passing options to the PHP binary.
You have a few options:
Set the variable globally on the system.
Set the variable on the command line before calling the script. This will persist in the environment after your script has finished executing, which you may not want.
Wrap the PHP script in another script, allowing you to create a temporary variable that exists only for the duration of the script.
Use a command line option instead of an environment variable.
The last two options are probably the cleanest way to do this, in that the variable created only exists for the run time of your script.
The implementation of option 1 is system dependent.
The implementation of option 2 is also system dependent - on Windows you would do set APPLICATION_ENV=staging&& php script.php and on *nix it would be export APPLICATION_ENV='staging' && php script.php.
If you were to go for option 3 you might be tempted to go for a shell script, but this is not portable (you would need a batch file for Windows and a shell script for *nix environments. Instead, I'd suggest you write a simple PHP wrapper script, something like this:
<?php
putenv('APPLICATION_ENV=staging');
include('script.php');
This allows you to leave your target script unchanged and set the environment variable for the script's session only.
A more complex wrapper script could easily be created which would allow you to specify variables on the command line, and even dynamically specify the script which should be executed when these variables are set.
Option 4 can be implemented using the $argv variable:
<?php
$applicationEnv = $argv[1];
// rest of you script
...and call the script like:
php script.php staging
However, it occurs to me that you seem to be indicating to the script which environment is running in (staging, dev, live, etc) - in which case it might be simplest to set a server-wide variable, and rename it as necessary to prevent collision with variables that other applications may be setting. That way you can simply invoke the script and not need to worry about this. This is assuming that you staging environment runs on a different machine to the live (which it should be).
When you execute a PHP script from the command line, it inherits the environment variables defined in your shell. That means you can set an environment variable using the export command like so:
export APPLICATION_ENV='staging'
Here is an example for setting one envirnnomental variable :
ENV_VAR='var' php script.php
Just in case you want to set multiple variables
Try this :
ENV_VAR1=1 ENV_VAR2=2 ENV_VAR3=3 php script.php
You can set a variable in /etc/environment like FOO="bar" which is then accessible with getenv() in both CLI and web requests. You may need to relog for this change to take effect.
Try using putenv and pass the variables through parameters
php script.php APPLICATION_ENV=staging
And in the script.php code:
for($i=1;$i<count($argv);$i++){
putenv($argv[$i]);
}
I have the same situation and i use next code (it works for me):
export APPLICATION_ENV=staging && php script.php
Hope it will helpful for you too.
Here is how I set the variable within my Apaches VirtualHost Container
SetEnv MY_VAR "/opt/"
Now, PHP can perfectly access this vairable:
echo $_SERVER['MY_VAR'];
/opt/
However, if I call a shell script from my PHP code
passthru('/path/to/myscript');
MY_VAR is empty within /path/to/myscript.
If I however modify the call like this, it works:
passthru('export MY_VAR='. $_SERVER['MY_VAR'] .'; /path/to/myscript');
Is there a better way to pass all environment variables to the shell script? Since I need to pass multiple ones.
I also tried system(), exec(), backticks and shell_exec(). They all show the same behavior, as passthru().
If you use putenv("MY_VAR=test"); the environment value is passed on to the invoked shell command. (but it is not put in $_ENV)
So you could simply do something like
foreach ( $_SERVER as $k=>$v ) putenv("$k=$v");
Try proc_open.
proc_open('/path/to/myscript', $descriptorspec, $pipes, null, $_SERVER);
You could pass an array with the environment variables as an option.
If you use phpinfo() in your first page, you will see that your env variable (MY_VAR) is not listed in the "Environment" box, but in the "Apache Environment" (if this is the webserver you use).
So, it seems that "getenv" not only gets its values from the environment, but also from Apache environment, which (it seems) is not inherited when launching something by passthru.
In the script executed by passthru, I only see env. vars listed in the "Environment" box, so it is consistent.
So, for each variable you want to export to the passthru script, you should use:
putenv('MY_VAR ='.apache_getenv('MY_VAR'));
Easy to do with an array and a for loop.
I guess it's because when you call SetEnv you're modifing your current shell environment. When you subsequently when you call passthru, your existing env is destroyed along with any non exported variables, and a "fresh" environment given to /path/to/myscript.
Can you instead pass env variables on the command line, as parameters to the /path/to/myscript -MY_VAR=/opt ?