Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP system() - return status is always 0

I need to get the following scripts running.

// File: script_a.php
<?php exit(1); ?>

// File: script_b.php
<?php 
     system('php script_a.php', $return);
     var_dump($return);
?>

Now my problem: On my windows system running script_b.php shows int(1) as expected. On our Unix-Server I always get int(0), what makes it impossible for me to check, if a certain failure happens inside the script_a.php.

Does anybody knows this problem and how to solve it?

like image 859
Sebastian Schmidt Avatar asked Nov 09 '10 08:11

Sebastian Schmidt


3 Answers

You might want to check if it's calling the right php executable on th Unix machine. On many UNIX systems you would need to call the php-cli executable insted of the php one for use on the command line. Another thing to check would be permissions. Maybe the user executing the script_b.php script doesn't have permissions to execute script_a?

like image 88
Seb Avatar answered Sep 20 '22 17:09

Seb


__halt_compiler() is called somewhere , able to check that ?

like image 44
ajreal Avatar answered Sep 21 '22 17:09

ajreal


Try making the PHP system call with the absolute path of both the PHP executable and the script filename, e.g.: system('/usr/bin/php /path/to/script_a.php', $return);. Maybe it's a path issue. (You may find the absolute path of your PHP executable with: which php).

Also, as someone suggested, try debugging the actual return value of script_a.php on your UNIX server by running php script_a.php; echo $? on the command line. That echo will output the last return value, i.e., the value returned by script_a.php.

Anyway, I suggest doing an include with a return statement as described in Example #5 of the include() documentation. If you can adapt your scripts like this, it's a more efficient way of communicating them.

// File: script_a.php
<?php return 1; ?>

// File: script_b.php
<?php 
     $return = (include 'script_a.php');
     var_dump($return);
?>
like image 42
scoffey Avatar answered Sep 17 '22 17:09

scoffey