确保 URL 指向真实位置?

发布于 2024-11-02 15:06:32 字数 265 浏览 2 评论 0原文

我已经启动了这个小小的 CMS,用户可以在提交之前预览帖子。我想要一个功能来检查输入的 URL 是否有效(IE 指向存在的资源,而不是它的格式是否正确)。最好,我想使用 JavaScript 来完成(那么我不必使用 AJAX 和 PHP),但 PHP 也可以。

有人可以指出我这样做的方法吗?以防万一,我希望通过运行 UNIX ping 命令等得到是/否响应。我不关心 URL 另一端的内容,只关心它是否确实指向可导航的内容。

谢谢,

詹姆斯

I've got this little CMS thing going, and the user has the ability to preview the post before submitting. I'd like to have a feature that checks whether a URL entered is valid (I.E. points to a resource that exists, not whether it's in the correct format or not). Preferably, I'd like to do it with JavaScript (I don't have to use AJAX and PHP then), but PHP is fine.

Can someone point me towards a way of doing this? Just in case, I want a yes/no response from something like running the UNIX ping command. I don't care about what's at the other end of the URL, just whether it actually points to something navigable.

Thanks,

James

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

橪书 2024-11-09 15:06:32

您可以简单地使用 fopen 来查看该 url 是否有效...当然,这可能会导致资源过度使用,具体取决于您执行此操作的频率。

http://php.net/manual/en/function.fopen.php

或者,您可以使用 jquery 插件:

http://binarykitten.me.uk/dev/jq-plugins/88-jquery-plugin-ajax-head-request.html

另一种方法,可能是最简单的方法t live ...但是您可以使用 jquery 调用另一个实现它的 php 脚本:

http://php.net/manual/en/function.get-headers.php

you could simply use fopen to see if the url will work ... of course, this could be resource overkill depending on how often you are doing it.

http://php.net/manual/en/function.fopen.php

Alternately, you can use a jquery plugin:

http://binarykitten.me.uk/dev/jq-plugins/88-jquery-plugin-ajax-head-request.html

An alternate, and probably the easiest way which isn't live ... but you can use jquery to call another php script that implements it:

http://php.net/manual/en/function.get-headers.php

折戟 2024-11-09 15:06:32

将其加载到新的 img 对象中,然后查看响应是什么。由于您实际上并未显示图像,因此您可能加载纯文本或其他非图像数据这一事实大多无关紧要,您只想查看图像请求的 HTTP 状态代码是什么。

这样做的好处是,如果恶意用户尝试加载指向大得惊人的文件的 URL,他们自己只会执行 DoS,因为请求将从他们的浏览器发出,而不是从您的服务器发出。不利的一面是,没有什么可以阻止他们欺骗正确的响应,并且仍然投入大量资源并提交表单。

Load it into a new img object, and see what the response is. Since you're not actually displaying the image, the fact that you may be loading up plain text or other non-image data is mostly irrelevant, you just want to see what the HTTP status code for the image request is.

The benefit is that if a malicious user tries to load in a URL that points to an obscenely large file, they'd only DoS themselves, as the request would go out from their browser and not your server. On the down side, nothing would stop them from spoofing a proper response and STILL put in that large-size resource and submit the form anyways.

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