使用 php 将 URL 变量传递给 exec()

发布于 2024-09-11 04:10:37 字数 388 浏览 2 评论 0原文

我有一个专用服务器,用来处理大量数据。按照我现在的方式,我可以打开一个带有进程 ID(如 example.php?ex_pid=123)的脚本,然后就可以了。它下载一小部分数据,对其进行处理,然后将其上传到数据库中,然后再次启动。

理想情况下,我想直接调用 example.php?ex_pid=123 而不是通过将变量传递给 example.php,如 exec('./example.php'.' '.EscapeShellArg ($variable)); 以防止其在全球范围内起作用。

我不关心输出,如果它可以在后台执行,那就太好了。顺便说一句,服务器是 Ubuntu 发行版。

这可能吗?如果是这样,任何帮助和示例将不胜感激。

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 to 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

零時差 2024-09-18 04:10:37

你可以这样做:

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

然后在 example.php 中做这样的事情:

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

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]))
孤星 2024-09-18 04:10:37

主要问题是 ?ex_pid 是 GET 数据,通常与包含文件或通过浏览器访问它相关。如果您包含该文件或从 Web 浏览器访问它,这将是微不足道的,但不幸的是,作为 CLI 运行它,您唯一的选择就是将其作为参数传递。您可以将其作为 ex_pid=123 传递并仅解析该数据,但仍然需要将其作为参数传递,但这样做您可以使用 parse_str() 来解析它。

根据脚本的作用,您可以调用 lynx 来调用附加了获取数据的实际页面,并为运行该脚本所需的 apikey 生成哈希值。不确定这是否是一个选项,但这是另一种按照您想要的方式进行操作的方法。

希望有帮助!

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!

江挽川 2024-09-18 04:10:37

我遇到了一个真正的问题,无法让它运行像 example.php?variable=1 这样的东西。

然而,我可以使用 exec 命令来运行一个单独的文件,而末尾没有 ?variable=1 。

我决定做的是根据我想要发送的变量动态更改模板文件的内容。该文件称为 template.php,包含通常作为 $_GET 运行的所有代码。不要使用 $_GET,而是在顶部设置变量的值。然后搜索该行代码并将其替换为您选择的任何值。

然后我保存了这个新文件并运行它。

在下面的示例中,我需要更改 SQL 查询 - 模板文件包含行 $sql="ENTER SQL CODE HERE";。我还需要更改顶部 aa 变量的值。
template.php 中的行是 $myvar=999999;下面的代码将 template.php 中的这些行更改为新值。

//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 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);
司马昭之心 2024-09-18 04:10:37

我完全忘记了这个问题,直到 @Andrew Waugh 对此发表评论(并且我收到了一封电子邮件提醒)。

无论如何,这个问题源于对使用 CLI 时如何将 $argv 数组传递给脚本的误解。您几乎可以根据需要使用任意数量的参数。我现在实现这一点的方式是这样的:

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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文