PHP Custom function to get search engine source keywords method _php tips

Source: Internet
Author: User
Tags exception handling php error

This article describes the PHP custom function to get search engine source keyword method. Share to everyone for your reference, specific as follows:

Get the search engine source keyword function:

function GetKeywords () {//Search engine keyword mapping static $host _keyword_map = Array (' www.baidu.com ' => ' wd ', ' v.ba  Idu.com ' => ' word, ' image.baidu.com ' => ' word ', ' news.baidu.com ' => ' word ', ' www.so.com ' =>  ' Q ', ' video.so.com ' => ' Q ', ' image.so.com ' => ' Q ', ' news.so.com ' => ' Q ', ' www.sogou.com '
  => ' query ', ' pic.sogou.com ' => ' query ', ' v.sogou.com ' => ' query ',);
  Check if the source is search engine if (!isset ($_server[' Http_referer '))) {return ';
  } $urls = Parse_url ($_server[' http_referer '));
  if (!array_key_exists ($urls [' Host '], $host _keyword_map)) {return ';
  $key = $host _keyword_map[$urls [' Host ']];
  Check that the keyword parameter exists if (!isset ($urls [' query '])) {return ';
  } $params = Array ();
  Parse_str ($urls [' query '], $params);
  if (!isset ($params [$key])) {return ';
  } $keywords = $params [$key];
  Check code $encoding = mb_detect_encoding ($keywords, ' UTF-8,GBK '); if ($encoding!= 'Utf-8 ') {$keywords = Iconv ($encoding, ' utf-8 ', $keywords);
return $keywords;

 }

function test:

<?php header ("content-type:text/html;
Charset=utf-8 "); $referers = Array (' http://www.baidu.com/s?cl=3&wd=%B9%E9%C0%B4&fr=vid1000 ', ' http://www.baidu.com/s?tn=92 506501_hao_pg&rtt=1&bsst=1&wd=%b9%e9%c0%b4 ', ' http://www.baidu.com/link?url= ctbhf7aaau6lwe61pjoeh-zhgum7d3yhymrm6xixjldqtmxciea7gg49s90q-qh8whd8ano-dpnhuawbbneewebtu8tomf5k1v7xy850etlpzymcs0e _y-scjp86im6e&wd=%e5%bd%92%e6%9d%a5&tn=baidu&ie=utf-8&inputt=2980 ', ' Http://www.baidu.com/link? Url=tin9nr6fwiy6iwwkccvf8hhhoxvuphqsyj1ydlqpy2roxktnsqs_3uxwvyjz2jpkpxf8-disorcspodum_jq2k&wd=%e5%bd%92%e6 %9d%a5&tn=baidu&ie=utf-8&input ', ' http://news.baidu.com/ns?cl=2&rn=20&tn=news&word=%E5% Bd%92%e6%9d%a5&ie=utf-8 ', ' yun_qi_img/error.html ', ' http://v.baidu.com/v?ct=301989888&s=25&ie=utf-8& Amp;word=%e5%bd%92%e6%9d%a5 ', ' http://www.so.com/s?ie=utf-8&shb=1&src=360sou_newhome&q=%E5%BD%92%E6 %9d%a5 ', ' http://video.so.com/v? q=%e5%bd%92%e6%9d%a5&src=tab_www ', ' http://image.so.com/v?q=%e5%bd%92%e6%9d%a5&src=tab_video& Fromurl=http%3a%2f%2fndent.oeeee.com%2fhtml%2f201309%2f16%2f258899.html ', ' http://news.so.com/ns?q=%E5%BD%92%E6 %9d%a5&src=tab_video ', ' http://www.sogou.com/web?query=%E5%BD%92%E6%9D%A5&_asf=www.sogou.com&_ast= 1402284372&w=01019900&p=40040100&ie=utf8&sut=6558&sst0=1402284372272&lkt=0%2c0%2c0 ', ' http://www.sogou.com/web?query=%E5%BD%92%E6%9D%A5&_asf=www.sogou.com&_ast=1402284372&w=01019900 &p=40040100&ie=utf8&sut=6558&sst0=1402284372272&lkt=0%2c0%2c0 ', ' http://pic.sogou.com/d? Query=%b9%e9%c0%b4&mood=0&picformat=0&mode=1&di=0&w=03021800&dr=1&did=1 ', ' http:// V.sogou.com/v?query=%b9%e9%c0%b4&p=&w= ', ' http://www.baidu.com/s?aaa=bbb ', ' http://www.baidu.com/', ' H
ttp://www.jb51.net/',);
  foreach ($referers as $r) {$_server[' http_referer '] = $r; Echo GetKeywords (), "\ n";

 }

Search engine occupancy ratio:

http://engine.data.cnzz.com/

More interested in PHP related content readers can view the site topics: "PHP string (String) Usage summary", "PHP array Operation techniques Encyclopedia", "PHP error and Exception handling method summary", "PHP operation and operator Usage Summary", " PHP Network Programming Skills Summary, "Introduction to PHP Basic Grammar", "PHP object-oriented Programming Introductory Course", "Php+mysql Database Operation Introduction" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.