-2

Below scripts working fine in CMD window but not in browsers. Below scripts are in 'front.php', win10's command window command 'php front.php' doing nice but http://localhost/blahblah/front.php sucking me by not triggering 'back.php'. Spent almost half a night trying all possibilites include shell_exec, proc_open but all ending up with above issue. Basic permission, php.ini related issues are all taken care.

pclose(popen('powershell.exe "Start-Process php -ArgumentList \'back.php\' -WindowStyle Hidden"','r'));

echo exec('D:\Wamp\php\php.exe D:\Wamp\apache2\htdocs\MySite\admin\back.php > output.txt 2>&1 echo $!', $pid);

Update

Just to narrow down, I'm using Win 10 Admin user and all WAMP setup is in my local pc only, using PHP 5.6.x. Both front.php and back.php are in same working folder and front able to write to file but back.php not executed from browser. From CMD the same is working!

front.php:

<?php
ini_set("display_errors",1);
error_reporting(E_ALL);
file_put_contents('timelog.txt', date('Y-m-d/H:i:s.u')); 

echo exec('php back.php > output.txt 2>&1 echo $!');
// pclose(popen('powershell.exe "Start-Process php -ArgumentList \'back.php\' -WindowStyle Hidden"','r'));
?>

back.php:

<?php
sleep(5);   // some delay
file_put_contents('timelog.txt', date('Y-m-d/H:i:s.u'). "\n"); 
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Raju
  • 75
  • 10
  • Patently, the "usual issues" of permissions and php.ini settings have not been taken care of. – symcbean Oct 06 '17 at 21:56
  • Your paths in the second line (`D:\Wamp\php\php.exe` and `D:\Wamp\apache2\htdocs\MySite\admin\back.php`) are faulty because you've not doubled-up your backslashes. In your first line you have used them as an escape character, and you need to do so in your second line as well. The sequence you need is double-backslash for each one. This may not be your problem, but it is worth fixing. – halfer Oct 06 '17 at 21:56
  • Next, try `echo exec('D:\\Wamp\\php\\php.exe -v')` on its own, and see if you get any output. If you do, try the next bit, etc. Also, you don't need to do a `>` redirection - PHP can do that in the `exec` command itself. – halfer Oct 06 '17 at 21:58
  • On a meta note, please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Oct 06 '17 at 21:58
  • symscbean, i took care of usual permissions related stuff, safe_mode, disabled_functions, etc in first place. As i said i'm able to run it in CMD and all my stuff is in single admin user locally. Also front.php able to write but back.php doing nothing, note all my stuff is in same folder so i don't see anything to do further with permissions. – Raju Oct 06 '17 at 22:19
  • halfer, infact all my environment variables were properly set and even infact i first tried my stuff without paths anywhere. PHP path well set, both front.php & back.php are in same folder. – Raju Oct 06 '17 at 22:21
  • Ran below front.php in CMD, it displayed: with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies Check! Ran the same in Browser, it just displayed: Check! – Raju Oct 06 '17 at 22:30
  • another point i noticed when i tried with $handle = popen('php back.php 2>&1', 'r'); in CMD is - back.php executed but didn't detached from front.php. As usual, when i run front.php in browser, back.php didn't executed at all. Thanks for any help on this. – Raju Oct 07 '17 at 00:14
  • Aside: always use `@halfer` to reply to me (without the `@` device, it won't notify me, auto-completion is available). – halfer Oct 07 '17 at 09:42

1 Answers1

0

There are a lot of problems with this.

I notice that back.php does not send anything to stdout or stderr (unless there are errors with the file op).

Also, your two scripts are writing timestamps to one file, and they are not in append mode, so one will overwrite the other. Try logging to different file names, or use FILE_APPEND as the third parameter in file_put_contents().

Finally it is not clear what the echo should do in php back.php > output.txt 2>&1 echo $!. That looks like a syntax error to me.

To investigate the nature of redirection in the console versus a browser, I wrote these two files:

inner.php

<?php
// Writes something to stdout and stderr

echo "Stdout\n";
fwrite(STDERR, "Stderr\n");

outer.php

<?php
// Execs the inner file with and without redirection

exec('php inner.php', $output);
print_r($output);

exec('php inner.php 2>&1', $output2);
print_r($output2);

On the console I get this:

Stderr
Array
(
    [0] => Stdout
)
Array
(
    [0] => Stdout
    [1] => Stderr
)

You can see in the first case, the stderr output is rendered to the console as part of the exec() output itself, i.e. it is less trappable unless we do the 2>&1 redirection.

I then span up a web server:

php -S localhost:10000

and visited the address: http://localhost:10000/outer.php.

I received this in my browser:

Array ( [0] => Stdout ) Array ( [0] => Stdout [1] => Stderr )

and the stray error appears in console output:

PHP 7.0.22-0ubuntu0.16.04.1 Development Server started at Sat Oct  7 11:01:43 2017
Listening on http://localhost:10000
Document root is /home/jon/Development/Personal/Missive
Press Ctrl-C to quit.
Stderr
[Sat Oct  7 11:01:56 2017] 127.0.0.1:54392 [200]: /outer.php
[Sat Oct  7 11:01:56 2017] 127.0.0.1:54394 [404]: /favicon.ico - No such file or directory

Thus, I think everything is working fine here.

You may wish to create a simple reproducible test case - with a large list of edits and comment updates, your question is becoming rather too convoluted to answer. There may also be value in explaining what you're trying to achieve, in case it's an X-Y problem.

halfer
  • 19,824
  • 17
  • 99
  • 186