“无法打开套接字”

发布于 2024-09-08 20:10:36 字数 608 浏览 3 评论 0原文

如何缓解我的网站上发生的“无法打开套接字”错误?

我遇到了问题,它是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅气称霸 2024-09-15 20:10:36

我的服务器在执行 fsocketopen 命令时是否遇到问题?

确实如此——尽管这并不一定意味着您的服务器有问题。这只是意味着您的服务器和 recaptcha 服务器之间的某个位置存在网络通信问题,导致套接字连接无法打开。

这可能有很多事情。这可能是您的代码或服务器上的配置问题(特别是如果服务器上的配置的某些方面是动态的),可能是服务器的连接级别问题,也可能是网络配置问题您的服务器托管的问题,可能是您的服务器和验证码服务器之间任何地方的网络配置问题,可能是它们托管的带宽问题,也可能是它们这边的配置问题。您可能需要使用 fsockopen 的额外错误报告参数来查看是否可以获得任何有意义的消息。您还可以在完全不同网络上的至少 2-3 个不同服务器上尝试您的设置——这也可以为您提供有关问题所在的具体指示。

不过,另一个问题是你将如何处理这种事情。 fsockopen 有时无法获得连接,因为即使在配置最好的网络环境中,也无法保证通信。硬件故障、事故发生、网络管理员手足无措、远程服务器陷入混乱、全球热核战争可能摧毁数据中心——你永远不知道。因此,您必须编写代码(并管理您的设置),以便在发生故障时有后备情况,并显示最终用户可以接受的错误消息。

您可能需要研究 PHP 的 set_error_handler 函数,并设置一个在发生 fsockopen 失败时调用的函数。在某些情况下,我喜欢使用它来触发异常,如下所示:

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);

通过该设置,您可以管理 fsockopen 连接,如下所示:

try {
   fsockopen('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
}

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 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 where fsockopen 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 fsockopen connections something like this:

try {
   fsockopen('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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文