Solved

PHP fsockopen causing Access Violation errors in IIS

Posted on 2008-10-29
6
645 Views
Last Modified: 2013-12-13
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
Comment
Question by:medfacit
  • 3
  • 2
6 Comments
 
LVL 19

Expert Comment

by:v2Media
ID: 22829444
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 22830258
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
 

Author Comment

by:medfacit
ID: 22839838
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
Technology Partners: 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!

 

Author Comment

by:medfacit
ID: 22839847
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
 
LVL 19

Accepted Solution

by:
v2Media earned 500 total points
ID: 22841049
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
 

Author Comment

by:medfacit
ID: 22854847
Problem solved. It was a misconfiguration.
Reinstalled PHP seems to have fixed the problem.
Thanks.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question