“无法打开套接字”
如何缓解我的网站上发生的“无法打开套接字”错误?
我遇到了问题,它是 CAPTCHA (我正在使用 reCAPTCHA)。它仅在我使用 reCAPTCHA 的两个页面上显示此错误。
我一直在生成新的密钥集,有时有效,有时无效。例如,它可以在 Safari 上运行,有时不能,但在 Firefox,反之亦然,它对我有用,但对我的合作伙伴之一不起作用,反之亦然。
我该如何解决这个问题?难道是我的服务器在执行 fsocketopen 命令时遇到问题?如果是这样,我该如何解决这个问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确实如此——尽管这并不一定意味着您的服务器有问题。这只是意味着您的服务器和 recaptcha 服务器之间的某个位置存在网络通信问题,导致套接字连接无法打开。
这可能有很多事情。这可能是您的代码或服务器上的配置问题(特别是如果服务器上的配置的某些方面是动态的),可能是服务器的连接级别问题,也可能是网络配置问题您的服务器托管的问题,可能是您的服务器和验证码服务器之间任何地方的网络配置问题,可能是它们托管的带宽问题,也可能是它们这边的配置问题。您可能需要使用
fsockopen
的额外错误报告参数来查看是否可以获得任何有意义的消息。您还可以在完全不同网络上的至少 2-3 个不同服务器上尝试您的设置——这也可以为您提供有关问题所在的具体指示。不过,另一个问题是你将如何处理这种事情。
fsockopen
有时无法获得连接,因为即使在配置最好的网络环境中,也无法保证通信。硬件故障、事故发生、网络管理员手足无措、远程服务器陷入混乱、全球热核战争可能摧毁数据中心——你永远不知道。因此,您必须编写代码(并管理您的设置),以便在发生故障时有后备情况,并显示最终用户可以接受的错误消息。您可能需要研究 PHP 的
set_error_handler
函数,并设置一个在发生fsockopen
失败时调用的函数。在某些情况下,我喜欢使用它来触发异常,如下所示:通过该设置,您可以管理
fsockopen
连接,如下所示: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: