Solved

PHP fsockopen causing Access Violation errors in IIS

Posted on 2008-10-29
6
636 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 108

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now