开发者

passing URL variables to exec() with php

I have a dedicated server that I use to crunch lots of data. The way I have it now, I can open a script with a process ID like example.php?ex_pid=123 and just let it go. It downloads a small portion of data, processes it, then uploads it into a database then starts again.

Ideally, I would like t开发者_Go百科o call example.php?ex_pid=123 directly and not by passing a variable to example.php like exec('./example.php'.' '.EscapeShellArg($variable)); to keep it from acting globally.

I don't care about the output, if it could execute in the background, that would be brilliant. The server is an Ubuntu distribution btw.

Is this even possible? If so, any help and examples would be more then appreciated.


You could do something like:

exec("./example.php '".addslashes(serialize($_GET))."');

And then in example.php do something like this:

count($_GET) == 0 && $_GET = unserialize(stripslashes($_SERVER['argv'][1]))


The main issue with that is that ?ex_pid is GET data which is generally associated with either including the file or accessing it through a browser. If you were including the file or accessing it from a web browser this would be trivial, but running it as CLI, your only option would be to pass it as an argument, unfortunately. You can pass it as ex_pid=123 and just parse that data, but it would still need to be passed as an argument but doing that you could use parse_str() to parse it.

Depending on what the script does, you could call lynx to call the actual page with the get data attached and generate a hash for an apikey required to make it run. Not sure if that is an option, but it is another way to do it how you want.

Hope that helps!


I had a real problem with this and couldn't get it to work running something like example.php?variable=1.

I could however get an individual file to run using the exec command, without the ?variable=1 at the end.

What I decided to do was dynamically change the contents of a template file , depending on the variables I wanted to send. This file is called template.php and contains all the code you would normally run as a $_GET. Instead of using $_GET, set the value of the variable right at the top. This line of code is then searched and replaced with any value you choose.

I then saved this new file and ran that instead.

In the following example I needed to change an SQL query - the template file has the line $sql="ENTER SQL CODE HERE";. I also needed to change the value of a a variable at the top. The line in template.php is $myvar=999999; The code below changes these line in template.php to the new values.

//Get the base file to modify - template.php
$contents=file_get_contents("template.php");
$sql="SELECT * FROM mytable WHERE foo='".$bar."'";

$contents=str_replace("ENTER SQL CODE HERE",$sql,$contents);
//Another search
$contents=str_replace("999999",$bar,$contents);

$filename="run_standalone_code".$bar.".php";

//If the file doesnt't exist, create it
if(!file_exists($filename)){
file_put_contents($filename, $contents);
}

//Now run this file
$cmd="/usr/local/bin/php ".$filename." >/dev/null &";
exec($cmd);


I had completely forgotten about this question until @Andrew Waugh commented on it (and I got an email reminder).

Anyways, this question stemmed from a misunderstanding as to how the $argv array is communicated to the script when using CLI. You can pretty much use as many arguments as you need. The way I accomplish this now is like:

if (isset($argv)) {
    switch ($argv[1]) {
        case "a_distinguishing_name_goes_here":
            $pid = $argv[2];
            sample_function($pid);
            break;
        case "another_name_goes_here":
            do_something_else($argv[2]);
            break;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜