問題描述
這可能是一個愚蠢的問題,但是否可以捕獲用戶在 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.
這篇關于是否可以從 Google 搜索中捕獲搜索詞?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!