$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - proc_open() fires twice|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - proc_open() fires twice

matteradmin9PV0评论

A WordPress post is supposed to process sample data (uploaded as PDF files) and displays the data (after some manipulations and reformatting) on the same post just below.

...
<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="filepdf" />
    <input type="submit" name="submit" value="Upload data samples (.pdf file)" />
</form>
...

I use the PHP code snippets (Insert PHP) plugin to include PHP code.

...
[insert_php]
    if(isset($_FILES['filepdf'])) {
        $mTime = number_format(microtime(true), 3, '.', '');
        $file_name = $_FILES['filepdf']['name'];
        $file_size = $_FILES['filepdf']['size'];
        $file_tmp = $_FILES['filepdf']['tmp_name'];
        $file_ext = strtolower(end(explode('.', $_FILES['filepdf']['name'])));
...
        $cmd = "./mybinary '$file_name'";
            $outfile = tempnam("/tmp", "cmd");
            $errfile = tempnam("/tmp", "cmd");
            $descriptorspec = array(
                0 => array("pipe", "r"),
                1 => array("file", $outfile, "w"),
                2 => array("file", $errfile, "w")
            );
            move_uploaded_file($file_tmp, $file_name);
            $proc = proc_open($cmd, $descriptorspec, $pipes);
...
[/insert_php]

Hours of troubleshooting, but I don't find why this happens ... when I upload a PDF file, the binary mybinary in proc_open() fires twice - sometimes (spaced by +/- 200 ms). Let's say 95% of all requests work fine, but 5% fail due to these double triggers. It seems (?!) that mobile Android devices using Chrome are more affected than other operating systems and/or browsers (not sure at all about that though). This is at least what Apache's access.log says. The present situation is unacceptable since the binary uses millisecond timecode generated by the PHP code (see above), and this messes up the entire process.

Questions:
1. Is my assumption correct that Chrome sends two requests?
2. If yes, why?
3. How can I avoid that?

I googled about that, and indeed, it seems that proc_open() sometimes fires twice, but I didn't find any solution.

Also, I don't know if this is WordPress related, or a PHP subject.

A WordPress post is supposed to process sample data (uploaded as PDF files) and displays the data (after some manipulations and reformatting) on the same post just below.

...
<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="filepdf" />
    <input type="submit" name="submit" value="Upload data samples (.pdf file)" />
</form>
...

I use the PHP code snippets (Insert PHP) plugin to include PHP code.

...
[insert_php]
    if(isset($_FILES['filepdf'])) {
        $mTime = number_format(microtime(true), 3, '.', '');
        $file_name = $_FILES['filepdf']['name'];
        $file_size = $_FILES['filepdf']['size'];
        $file_tmp = $_FILES['filepdf']['tmp_name'];
        $file_ext = strtolower(end(explode('.', $_FILES['filepdf']['name'])));
...
        $cmd = "./mybinary '$file_name'";
            $outfile = tempnam("/tmp", "cmd");
            $errfile = tempnam("/tmp", "cmd");
            $descriptorspec = array(
                0 => array("pipe", "r"),
                1 => array("file", $outfile, "w"),
                2 => array("file", $errfile, "w")
            );
            move_uploaded_file($file_tmp, $file_name);
            $proc = proc_open($cmd, $descriptorspec, $pipes);
...
[/insert_php]

Hours of troubleshooting, but I don't find why this happens ... when I upload a PDF file, the binary mybinary in proc_open() fires twice - sometimes (spaced by +/- 200 ms). Let's say 95% of all requests work fine, but 5% fail due to these double triggers. It seems (?!) that mobile Android devices using Chrome are more affected than other operating systems and/or browsers (not sure at all about that though). This is at least what Apache's access.log says. The present situation is unacceptable since the binary uses millisecond timecode generated by the PHP code (see above), and this messes up the entire process.

Questions:
1. Is my assumption correct that Chrome sends two requests?
2. If yes, why?
3. How can I avoid that?

I googled about that, and indeed, it seems that proc_open() sometimes fires twice, but I didn't find any solution.

Also, I don't know if this is WordPress related, or a PHP subject.

Share Improve this question edited Nov 7, 2018 at 6:59 geohei asked Oct 23, 2018 at 15:09 geoheigeohei 1112 bronze badges 5
  • 1 This will execute however many times the content is parsed, but keep in mind, the [insert_php] shortcode you're using is incredibly dangerous, it's one of the most dangerous things you can do to a site. Instead just implement a shortcode and put the PHP code inside an actual PHP file. Again, I cannot understate just how dangerous what you're doing is, and how stupendously dangerous plugins that let you do this are. – Tom J Nowell Commented Oct 23, 2018 at 15:32
  • Thanks for the comment! I try to get rid of [insert_php] then. I was completely unaware of that risk (WP newbie in PHP coding). Never wrote my own shortcode. Do I need a plugin to write/use my own shortcode? Is this a good site to get started? link. If not, please give me a starting point. Thanks! – geohei Commented Oct 23, 2018 at 19:52
  • No, you can create a plugin to do the shortcode, but a plugin is just a PHP file with a comment at the top in the plugins folder. A shortcode shouldn't be very difficult to write, especially if you already have PHP code written – Tom J Nowell Commented Oct 23, 2018 at 19:52
  • @TomJNowell - I don't really understand what is so dangerous in this PHP plugin, but since the code was not working properly, I gave the Shortcode concept a try. See below ... – geohei Commented Nov 6, 2018 at 7:58
  • The problem is that anybody who can use shortcodes or write to the database can execute PHP, no filesystem access required. In combination with other shortcode features, somebody could sneak in an insert_php shortcode and execute anything on the server – Tom J Nowell Commented Nov 6, 2018 at 20:35
Add a comment  | 

1 Answer 1

Reset to default 1

To complete this ... I initially used PHP code snippets (Insert PHP) plugin (version 2.0.6 - latest). The php page was in fact only called once, but executed twice. I didn't have any explanation for all this described above (same temp filename, but code called twice, ...).

After I changed the PHP code WordPress integration method into the Shortcode concept, all worked fine (till now). So I believe that this mentioned plugin was the reason for the double triggers?!

And no double clicks - I checked that already right from the beginning. So probably the PHP code itself was not the culprit.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far