views:

98

answers:

1

Hello, my question is how do I alleviate the "could not open socket error" that is happening on my site.

I have trouble shot that it is captcha (I'm using recaptcha)

It is only displaying this error on the two pages where I use recpatcha.

I have been generating new sets of keys and sometimes it works and sometimes it does not. For example it worked on safari sometimes but not firefox, vice versa, and worked for me and not for one of my partners, vice versa.

Any suggestions? Could it be that my server is having trouble doing the fsocketopen command? if so, how do I fix that?

Thanks

+1  A: 

Could it be that my server is having trouble doing the fsocketopen command?

Exactly -- although it doesn't necessarily mean that something is wrong with your server. It just means that somewhere between your server and the recaptcha server, there's a network communications problem that prevents the socket connection from being opened.

This could be a lot of things. It could be a config issue with your code or on your server, (particularly if there's some aspect of the configuration on your server that's dynamic), it could be an issue with the level of connectivity your server has, it could be a network config issue where your server is hosted, it could be a network configuration issue anywhere between your server and the recaptcha server, it could be a bandwidth issue where they're hosted, it could be a configuration issue on their side. You might want to use the extra error reporting arguments to fsocketopen to see if you can get any messages that make sense. You might also try your setup out on at least 2-3 different servers on totally different networks -- that could also give you a somewhat specific indication about where the problem is.

The other question, though, is how you're going to manage this kind of thing in general. fsocketopen` just sometimes fails to get a connection, because in even the best configured network environment, there's no communications guarantee. Hardware fails, accidents happen, network admins have fat-finger moments, remote servers get confused, global thermonuclear war can take out a data center -- you just never know. So you've got to write your code (and manage your setup) so you've got fallback cases for when failure happens and you display error messages that are acceptable for the end user.

You might want to look into PHP's set_error_handler function and set up a function to be called on occurrences where fsocketopen fails. In some situations, I've become fond of using it to trigger exceptions, something like this:

function throw_error_exception($number = 0, $str = '',$file = null,$line = null) {
   throw new ErrorException($str, 0, $number, $file, $line);
}

set_error_handler('throw_error_exception',E_ALL);

With that setup, you could manage fsocketopen connections something like this:

try {
   fsocketopen('remote.host.com',8080,$fso_errnum,$fso_errstr,30);
} catch(Exception $e) {
   // here you can look at properties/methods of $e, and $fso_* values, and 
   // figure out what nice error messages you want to display for your users
}
Weston C
great answer! Thanks for spending the time!
LightningWrist