php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47630 Unknown error using fsockopen() when with php-cgi through php cli
Submitted: 2009-03-12 06:06 UTC Modified: 2009-03-18 12:58 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: metala at metala dot org Assigned:
Status: Closed Package: Network related
PHP Version: 5.2.9 OS: Windows XP SP2
Private report: No CVE-ID: None
 [2009-03-12 06:06 UTC] metala at metala dot org
Description:
------------
The problem persists in 5.2.6, 5.2.9 and 5.2.9-1 in Windows only.
I have 2 script files, a parent.php and a child.php. The parent.php, which is running in CLI, starts the child.php with php-cgi.exe.
fsockopen(), or any other network function fails.

Reproduce code:
---------------
parent.php
<?php
$env = array (
  'SCRIPT_FILENAME' => dirname(__FILE__).'\\child.php',
);
$descriptorspec = array(
	0 => array("pipe", "r"),
	1 => array("pipe", "w"),
	2 => array("pipe", "w"),
);
$procName = 'php-cgi';
$proc = proc_open($procName, $descriptorspec, $pipes, null, $env, array('bypass_shell' => true));
$read = array($pipes[1]);
stream_select($read, $null = array(), $null = array(), 100);
echo fread($read[0], 1024);

child.php
<?php
$fp = fsockopen('127.0.0.1', 445, $errno, $errstr);
var_dump($errno, $errstr);

Expected result:
----------------
I expect no error to be returned, because 445 is opened and even telnet manages to connect.

Actual result:
--------------
H:\test\php>php parent.php
X-Powered-By: PHP/5.2.9-1
Content-type: text/html

<br />
<b>Warning</b>:  fsockopen() [<a href='function.fsockopen'>function.fsockopen</a
>]: unable to connect to 127.0.0.1:445 (Unknown error) in <b>H:\test\php\child.p
hp</b> on line <b>2</b><br />
int(0)
string(0) ""

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-03-12 13:01 UTC] metala at metala dot org
I tested it on another Windows XP machine and got the same result.

On the other hand with Debian GNU/Linux Squeeze I have no problems.
 [2009-03-16 19:53 UTC] johannes@php.net
Are you sure there is no firewall blocking?
 [2009-03-16 20:37 UTC] metala at metala dot org
I disabled it before testing.

I have another Windows XP machine I can test on. Results coming soon.
 [2009-03-16 21:04 UTC] metala at metala dot org
It failed again.

As I wrote in the description "any network function fails".
What I did was:
$fp = fsockopen('google.com', 80, $errno, $errstr);
And the result was:
H:\test\php>php parent.php
X-Powered-By: PHP/5.2.9-1
Content-type: text/html

<br />
<b>Warning</b>:  fsockopen() [<a href='function.fsockopen'>function.fsockopen</a
>]: php_network_getaddresses: getaddrinfo failed: No such host is known.  in <b>
H:\test\php\child.php</b> on line <b>2</b><br />
<br />
<b>Warning</b>:  fsockopen() [<a href='function.fsockopen'>function.fsockopen</a
>]: unable to connect to google.com:80 (php_network_getaddresses: getaddrinfo fa
iled: No such host is known. ) in <b>H:\test\php\child.php</b> on line <b>2</b><
br />
int(0)
string(69) "php_network_getaddresses: getaddrinfo failed: No such host is known.
 "


When I run it in DOS:
>SET SCRIPT_FILENAME=child.php
>php-cgi

There appears to be no error.
 [2009-03-18 12:50 UTC] metala at metala dot org
OK, problem solved.

The problem is in the Environment variables of the new process:
$env = array (
  'SCRIPT_FILENAME' => dirname(__FILE__).'\\child.php',
);
The missing variable is SystemRoot, which in my case is "C:\WINDOWS".
So with the final settings:
$env = array (
  'SCRIPT_FILENAME' => dirname(__FILE__).'\\child.php',
  'SystemRoot' => $_ENV['SystemRoot'],
);

Network functionality is working perfectly.
 [2009-03-18 12:58 UTC] metala at metala dot org
I don't think "bug" need a fix.
A note in the PHP manual, in proc_open() function or elsewhere, would be OK.
 [2010-06-14 11:29 UTC] webmaster at gleitgeb dot at
Is it possible just to add a check if the environment variables are correct/existing paths?
Otherwise the process should not start or it should add a comment in some logfile ..

That stupid problem caused me hours of unnecessary work because I could not find the connection between a wrong environment variable and a not working socket connection (in my case imap_open didn't work as it always said "Host not found")
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 14:01:29 2024 UTC