How do I alleviate the "Could not open socket" error that is happening on my site?
I have troubleshot that it is CAPTCHA (I'm using reCAPTCHA). It is only displaying this error on the two pages where I use reCAPTCHA.
I have been generating new sets of keys, and sometimes it works and sometimes it does not. For example, it worked on Safari and sometimes not, but on Firefox, and vice versa, and it worked for me and not for one of my partners and vice versa.
How can I fix this problem? Could it be that my server is having trouble doing the fsocketopen command? If so, how do I fix that?
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
fsockopen
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.
fsockopen
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 wherefsockopen
fails. In some situations, I've become fond of using it to trigger exceptions, something like this:With that setup, you could manage
fsockopen
connections something like this: