tags:

views:

31

answers:

4

I have the following PHP script:

<?php
  $fortune = `fortune`;
  echo $fortune;
?>

but the output is simply blank (no visible errors thrown).

However, if I run php -a, it works:

php > echo `fortune`;
Be careful of reading health books, you might die of a misprint.
        -- Mark Twain
php >

Am I missing a config directive or something that would cause this?

Edit: So, I tried running my script using $ php-cgi fortunetest.php and it worked as expected. Maybe the issue is with Apache2?

A: 

Honestly I don't know. However It might be you have to say echo $fortune instead.

Josh K
+2  A: 

What $PATH is Apache run with? Where does fortune live?

Phil P
A: 

the fortune command just outputs a quotation or famous quote for you. You can just simply store those quotations in a flat text file, or if you have a database, store them in a table. Then use the rand() function or similar in PHP to generate a random number and use this number to get that row in the quotations file/table. This way, your PHP script is not dependent on whether the system has the fortune command installed or not.

ghostdog74
A: 

Clearly, this isn't a programmer issue per-se, so I didn't want to post it on SO. Anyways, I found the solution: fortune lived in /usr/games, so I thought it might be a $PATH issue, but when I did su www-data and ran $ fortune it worked as expected, and /usr/games was in $PATH. Apparently, Apache was using a different $PATH variable even though it was running under user www-data, so I rewrote the script to use /usr/games/fortune instead of plain fortune and it worked. Since fortune wasn't the point of the script, it was kind of a waste of time, but lesson learned.

erjiang