php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44551 php script don't send output/STDOUT to apache
Submitted: 2008-03-27 15:28 UTC Modified: 2008-07-14 21:37 UTC
Votes:6
Avg. Score:4.5 ± 0.5
Reproduced:6 of 6 (100.0%)
Same Version:4 (66.7%)
Same OS:0 (0.0%)
From: laurent_baillif at hotmail dot com Assigned:
Status: Not a bug Package: Apache2 related
PHP Version: 5.2.5 OS: *
Private report: No CVE-ID: None
 [2008-03-27 15:28 UTC] laurent_baillif at hotmail dot com
Description:
------------
Hello,

I have an issue with STDOUT/OUTPUT of PHP. I?m about to use a PHP script for a RewriteMap directive inside APACHE. And I have identified that APACHE never receive the output from my PHP script and this after trying several way to send the output and also on several platform (winXP/apache2/PHP5 & OPENSUSE/apache2/PHP5.2.5)

Thank?s for your feedback
Regards,


Reproduce code:
---------------
I have this configuration in a virtual host of APACHE (Works fine if I put a Perl script instead of PHP)

<VirtualHost *:80>
    ServerAdmin mymail@mail.com
    DocumentRoot "/home/mywork/web/partenaire"
    ServerName mytest.mydomain.com
    DirectoryIndex index.php
ErrorLog /home/mywork/log/apache/error.log
LogLevel debug
RewriteLog /home/mywork/log/apache/rewrite.log
RewriteLogLevel 9

RewriteEngine on
#RewriteLock /home/mywork/web/partenaire/test/rewrite.lock
#RewriteMap rewritemap prg:/home/mywork/web/partenaire/test/rewritemap.perl
RewriteMap rewritemap prg:/home/mywork/web/partenaire/test/rewritemap.php
RewriteOptions Inherit

RewriteRule ^/test/(.*)$  ${rewritemap:%{REQUEST_URI}} [L]

        <Directory "/home/mywork/web/partenaire">
           Options FollowSymLinks
           AllowOverride all
           Order allow, deny
           Allow from all
        </Directory>
</VirtualHost>

I can confirm my PHP script is working fine from the console (read STDIN and write STDOUT) and when it is start with apache, he receive correctly the STDIN but never send back the STDOUT. I have tried several configurations in my script and APACHE never get the STDOUT and just hang or return a blank URL.

My rewritemap.php script looks like this:

#!/usr/bin/php

<?php

//Flag to verify the script is starting
//$handle = fopen ("home/stat.txt", 'a+');
//fwrite($handle, date("H")."h".date("i")."m".date("s").": start of the script\r\n");
//fclose($handle);

$stdin = fopen ('php://stdin', 'r');
$output = fopen('php://output', 'w');
ob_implicit_flush (true);  // Use unbuffered output

while ($line = fgets ($stdin)) {
        $line = trim($line);

//Flag to verify the script is receinving STDIN
//$handle = fopen ("home/stat.txt", 'a+');
//fwrite($handle, date("H")."h".date("i")."m".date("s").": Just receive: $line \r\n");
//fclose($handle);

    fwrite(STDOUT,"mynewURL?".$line);  //try this one and didn?t work with apache
    fwrite($output,"mynewURL?".$line);  //try this with OUTPUT and didn?t work with neither

    echo "mynewURL?".$line    //try this one also but only works with the console

}

?> 


Expected result:
----------------
I can confirm my PHP script is working fine from the console (read STDIN and write STDOUT) and when it is start with apache, he receive correctly the STDIN but never send back the STDOUT. 

With the same configuration a perl script is working fine for apache. I have tried several configurations in my php script and APACHE never get the STDOUT and just hang or return a blank URL.


Actual result:
--------------
Blank url or apache hang.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-06-25 11:37 UTC] john dot walsh at mini-net dot co dot uk
Hi,

I had a similar problem - stdout not getting to apache, but worked from the command line.

I tried switching to using echo (with carefully constructed strings), but that still didn't work.

Then, just to be sure, I put an echo "z" at the start (top level) of my script.

The 'z' came through, along will all the rest of my scripts output.

I changed the echo "z" to echo "" - the script continued to work.

So it seamed that stdout needed some kick-into-life usage, before it would work from my functions.

So something to try.

Note: I was outputting binary data, and I think the first character would have been value 0x0A.

Yours,
John.
 [2008-06-25 12:16 UTC] laurent_baillif at hotmail dot com
Hi,

I have found a solution for my issue! Always send your echo with a "\n" at the end of your script. 

We sould put that in the manuel because it is says no where.

Another issue I have experience:
If your script is running on a windows platform, never leave a magic cookies (from linux for instance) and never leave a blank line at the begining of your script. that will de-synchronise all your answer in STDOUT. 
Let me explain:
request one => answer one: error in apache
request two => answer two: request one in apache
request three => answer three: request two in apache
and so on...

So if you are running on windows, always start your script with the php quote:"<?php" and finish it with "?>" whithout any white space in the middle of the code (e.g like: "?>    <?php" )

Regards
 [2008-07-14 21:37 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 11:01:30 2024 UTC