什么会导致双页请求?
我目前正在调查我的网站上的双重请求问题。并非总是如此,但有时,请求的页面实际上会加载两次......这实际上不是问题,直到它位于使用 PHP 的页面上,该页面根据请求将内容插入我的数据库(我的跟踪脚本)。
我读过,图像标签中的空 src 和 css 背景中的空 url() 可能会导致页面被请求两次。
但是,我找不到这些问题。
还有其他什么可能导致这样的事情吗?
针对我的情况的回答
经过一番广泛的研究,事实证明,就我而言,第二个请求来自用户代理“Mediapartner-Google”。我开始注意到,在投放 Adsense 广告的页面上,在我自己访问该页面后的几秒钟内,我可以预期该爬虫会进行二次访问。
在没有 Adsense 广告的页面上似乎不会发生这种情况。
我将在下面标记一个答案,因为似乎在大多数情况下,这些都是需要检查的正确内容。
I am currently investigating a double request problem on my site. Not all the time, but sometimes, a requested page will in fact load twice...which is not a problem really until it is on a page with PHP that inserts stuff into my db on request (my tracking script).
I have read that an empty src in an image tag, and an empty url() in a css background could potentially cause the page to be requested twice.
However, I can't find any problems with those.
Is there anything else that could be causing something like this?
ANSWER FOR MY SITUATION
After some extensive research, it turns out that in my case specifically, the second request has been coming from the user agent "Mediapartner-Google". I began to notice that on pages that serve an Adsense ad, I could expect a secondary visit from this crawler within seconds after I visit the page myself.
This doesn't seem to be happening on pages without Adsense ads.
I am going to mark an answer below, because it seems like for most situations, those are the correct things to check.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我曾坐在我发誓比这更了解的人旁边,当他们双击我们应用程序中的每个超链接时,我惊愕地看着他们。
没花很长时间就弄清楚为什么他们的页面加载时间是其他人的两倍......
在实现更改后端状态的页面时,这样的事情肯定会让人停顿。很多人将序列号放在隐藏的表单元素中,以便后端可以检测到双重提交。
I have sat beside people whom I would swear knew better than this, and watch aghast as they double-clicked on every hyperlink in our app.
Didn't take long to figure out why they were experiencing double the page load time of everyone else...
Things like this certainly tend to give one pause when implementing pages that change the backend state. A lot of people put sequence numbers in hidden form elements so the backend can detect a double-submit.
我以前见过的原因:
缺少样式表或图像
如果您验证 HTML 等,Chrome/Firefox 的 Web 开发人员插件有时会请求两次
有时,很难追踪两次请求的根本原因。
无论哪种方式,您都不应该通过 GET 请求更改数据库状态(或会话状态)。您应该在没有 postdata 的情况下运行的唯一 SQL 查询是 SELECT。所有更新和插入都应使用表单完成,即使表单仅包含提交按钮。
The causes I've seen before:
Missing stylesheet or image
Web developer addon for Chrome/Firefox sometimes requests things twice if you're validating HTML etc.
Sometimes it's just too difficult to track down the root cause of a double request.
Either way, you should NOT be changing database state (or session state) through a GET request. The only SQL query you should be running without postdata is SELECT. All updates and inserts should be done using forms, even if the form consists only of a submit button.
某些浏览器上某些元素中的
src=""
(例如)可以再次请求当前页面。
src=""
in certain elements on certain browsers (such as<img src="" />
) can request the current page again.404 是看似被请求两次的请求的主要来源。检查你的 CSS、JS 和图像源是否正确。
404's are a prime source for a request seemingly being requested twice. Check your CSS, JS and image sources are all correct.
我们的 CMS 中有一个非常奇怪的行为,其中 jQuery 对话框灯箱中的 iframe 进行了双倍的数据库插入。
经过几个小时的调试和大声的WTF之后,我们终于确定了它。对话框关闭方法在销毁对话框之前将焦点设置到对话框的 iframe,并导致 iframe url 重新加载!
We had a very strange behaviour in our CMS where an iframe in a jQuery dialog lightbox made a doubled database insert.
After hours of debugging and loud WTFs we nailed it down. the dialog close method was setting the focus to the iframe of the dialog before destroying it and caused a reload of the iframe url!
我已经见过无数次了。互联网上充满了奇怪的人,他们不断双击他们遇到的所有内容。
您可以通过将全局双击事件侦听器附加到每个锚标记(标签)来在网站中停止此操作。
例如,如果您安装了 jQuery,则可以执行以下操作:
当然,这只是一个示例。您可以使用普通 Javascript 获得相同的结果。
这应该默默地忽略双击操作。
如果他们快速单击两次而不是双击,那么您可以使用可以限制页面中所有链接的单击手柄,以确保它们在 3 秒之内不能被单击多次。
I have seen this countless times. The internet is full of strange people who keep double-clicking on everything they come across.
You can stop this in you web site by attaching a global double-click event listener to every anchor tag ( tags).
For example, if you have jQuery installed, you can do the following:
This is just an example of course. You can achieve the same result using vanilla Javascript.
That should silently ignore the double click action.
In case they are fast clicking twice instead of double clicking, then you can use can throttle the click handle on all the links in the page to ensure that they cannot be clicked more than once within say ... 3 seconds.