How to set $_GET and $_POST variables of php through python? - php

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

Related

Export environment variable from PHP CLI

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

Make Powershell CLI (environment) variables available in PHP

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.

Is it possible to read environment variables from php?

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.

Setting PHP enviromental variable while running command line script

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.

How can I pass a url or variable from Perl to PHP?

I have two existing scripts that work fine as individuals.
The main script is Perl. I wish to execute the PHP script from a sub in the Perl script.
Usually, the PHP script is just run via direct url e.g. http://me.com/phpscript.php?foo=bar
I would like to just call the PHP script from the Perl and pass the foo var so, I don't need to hit the PHP script with my browser to process the data.
I am not talented enough to rewrite the PHP script to Perl.
I tried exec("http://me.com/phpscript.php?foo=bar"); and include and system to no avail.
I have read and searched but, found only solutions to call Perl from PHP.
I really appreciate the great guidance I always fine here.
Seems like LWP::UserAgent should work for this scenario.
require LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
my $response = $ua->get('http://me.com/phpscript.php?foo=bar');
if ($response->is_success) {
print $response->decoded_content; # or whatever
}
else {
die $response->status_line;
}
You can directly run a php file if you add #!PathToPhp
./myscript.php
in that file using argc or argv or args you can get arguments passed to this file most basic is args
#!/bin/php
<?php
foreach($args as $key => $value){
echo "\n".$key.":".$value;
If the script is located on the local filesystem, you should be able to exec it directly using the php interpreter and the path to the file. To access it via the web, use the LWP package.
For example:
exec('/usr/bin/php', 'myscript.php', #arguments);
Note that command-line arguments are handled differently than URL arguments; your PHP script will probably need to be modified to use them correctly.
There is a CPAN package that aims to provide a bridge between PHP and Perl:
This class encapsulates an embedded PHP5 intepreter. It provides proxy methods (via AUTOLOAD) to all the functions declared in the PHP interpreter, transparent conversion of Perl datatypes to PHP (and vice-versa), and the ability for PHP to similarly call Perl subroutines and access the Perl symbol table.
The goal of this package is to construct a transaparent bridge for running PHP code and Perl code side-by-side.
Not sure how stable this is though. See
PHP::Interpreter
Integrating PHP and Perl

Categories