Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 687
  • Last Modified:

PHP fsockopen causing Access Violation errors in IIS

Hi there,

I am trying to use fsockopen on a newly setup server running Windows 2003, IIS and PHP version 5.2.6. I can run this particular script once, but if I try to run it a second or third time, I get the following error:

PHP has encountered an Access Violation at 01B5B74E

I then tried to pin point in my code what was causing the error, and it seems to be the fsockopen command. To test this, I hopped on the PHP site, copied their example code for fsockopen and ran. Again, it worked the first time but caused the error the second time.

I have attached the code I am trying to run (directly from php.net/fsockopen). The segment in my own script is very similar, connecting to a server > getting some info then closing the socket.

Can someone please assist in explaining what could be causing this script to run once but fail on subsequent runs.

Thanks.
<?php
$fp = fsockopen("www.google.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
 
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>

Open in new window

0
medfacit
Asked:
medfacit
  • 3
  • 2
1 Solution
 
v2MediaCommented:
I believe it's a threading issue, especially if you're using an ssl socket. There's a bug report somewhere; I remember reading it at bugs.php.net a little while back.

As a work-around, why not try using Curl with the option CURLOPT_RETURNTRANSFER as true.
http://php.net/curl
0
 
Ray PaseurCommented:
FWIW, Here is the fsockopen code from my PayPal IPN Script.  It works on my Linux servers. ;-)

Your workaround might include CURL, or if you need only GET and not POST, you can just do file_get_contents( - URL - )

HTH, ~Ray
<?php // paypal_ipn.php - custom PayPal IPN processor
 
// READ THE POST FROM PayPal AND ADD 'cmd'
$req		= 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
	$value		= urlencode(stripslashes($value));	// ENCODE FOR BOUNCE-BACK
	$req		.= "&$key=$value";
}
 
// POST BACK TO PayPal SYSTEM TO VALIDATE
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
 
 
// TEST FOR VERIFICATION
if (!$fp) { /* HTTP ERROR */ }
 
// HTTP OPEN - WRITE HEADER AND REQUEST
fputs ($fp, $header . $req);
 
// HTTP OPEN - READ PayPal RESPONSE, DISCARDING HEADERS TO THE VERY END
$paypal_reply 	= '';
$paypal_headers	= '';
while (!feof($fp)) {
	$paypal_reply	= fgets ($fp, 1024);
	$paypal_headers	.= $paypal_reply;
}
fclose ($fp);
 
// IF THIS IS TRULY A POST FROM PAYPAL, PROCESS ORDER NOTIFICATION
if (strcmp ($paypal_reply, "VERIFIED") == 0) { /* IT IS VERIFIED */ }

Open in new window

0
 
medfacitAuthor Commented:
Hi there,

I have tried your suggestion v2Media and still get the same Violation error. I have attached the code that is causing this error.

Thanks.
<?php
 
$url = "http://www.domain.com/target.php";
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_URL,$url);
 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,"field=value");
 
$result= curl_exec ($ch);
curl_close ($ch);
print $result;
 
?>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
medfacitAuthor Commented:
Should have mentioned that it works only once like fsockopen then causes the violation error. Exactly the same result would indicate a problem with the server/firewall?

Thanks.
0
 
v2MediaCommented:
Perhaps run through php.ini configuration settings, extension files etc for your php install on IIS. It sounds like a misconfiguration of php.

php.net/install.windows
0
 
medfacitAuthor Commented:
Problem solved. It was a misconfiguration.
Reinstalled PHP seems to have fixed the problem.
Thanks.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now