是否可以从 Google 搜索中捕获搜索词?

2021-12-20 00:00:00 search http-referer php html

这可能是一个愚蠢的问题,但是否可以捕获用户在 Google 搜索框中键入的内容,以便随后可以使用它在我网站的登录页面上生成动态页面?

This may be a stupid question, but is it possible to capture what a user typed into a Google search box, so that this can then be used to generate a dynamic page on the landing page on my Web site?

例如,假设有人在 Google 上搜索热狗",而我的网站作为搜索结果链接之一出现.如果用户单击将他们定向到我的网站的链接,我是否有可能以某种方式从 Google 搜索框中知道或捕获热狗"文本,以便我可以调用一个脚本来搜索我的本地数据库中的内容与热狗有关,然后显示?这对我来说似乎完全不可能,但我真的不知道.谢谢.

For example, let's say someone searches Google for "hot dog", and my site comes up as one of the search result links. If the user clicks the link that directs them to my Web site, is it possible for me to somehow know or capture the "hot dog" text from the Google search box, so that I can call a script that searches my local database for content related to hot dogs, and then display that? It seems totally impossible to me, but I don't really know. Thanks.

推荐答案

是的,这是可能的.请参阅 HTTP 标头 Referer.Referer 标头将包含 Google 搜索结果页面的 URL.

Yes, it is possible. See HTTP header Referer. The Referer header will contain URL of Google search result page.

当用户点击 Google 搜索结果页面上的链接时,浏览器将使用这种 HTTP 标头向您的网站发出请求:

When user clicks a link on a Google search result page, the browser will make a request to your site with this kind of HTTP header:

Referer: http://www.google.fi/search?hl=en&q=http+header+referer&btnG=Google-search&meta=&aq=f&oq=

只需从请求头解析 URL,用户使用的搜索词将在 q - 参数中.上面例子中使用的搜索词是http header referer".

Just parse URL from request header, the search term used by user will be in q -parameter. Search term used in above example is "http header referer".

同样的方法通常也适用于其他搜索引擎,它们只是在 Referer 标头中有不同类型的 URL.

Same kind of approach usually works also for other search engines, they just have different kind of URL in Referer header.

这个答案 展示了如何在 PHP 中实现这一点.

This answer shows how to implement this in PHP.

Referer 标头仅适用于 HTTP 1.1,但这几乎涵盖了所有现代浏览器.浏览器也可能会伪造Referer header或者header可能完全丢失,所以不要根据Referer header做太认真的决定.

Referer header is only available with HTTP 1.1, but that covers just about any somewhat modern browser. Browser may also forge Referer header or the header might be missing altogether, so do not make too serious desicions based on Referer header.

相关文章