search
Homephp教程PHP源码搜索引擎来源关键字分析程序

<?php
/**
 * 分析搜索引擎到来的关键字
 * 
 * 
 * @author zhangjun
 * @charset utf-8
 */
class searchTerms {
    public static $banUrl = array(&#39;qq.com&#39;,&#39;localhost&#39;); //不解析的referer 有利于加快程序处理速度
    public static $parseHost = array(&#39;baidu&#39;,&#39;google&#39;,&#39;360&#39;,&#39;soso&#39;);  //能解析的referer
    /**
     * 主方法
     * @return boolean || array
     */
    public static function keyword(){
        if(!isset($_SERVER[&#39;HTTP_REFERER&#39;]))
            return false;
        $referer = trim($_SERVER[&#39;HTTP_REFERER&#39;]);
        $refererArr = parse_url($referer);
        //判断refer是否来至不需要分析的地址。
        if(self::inBanUrl($refererArr[&#39;host&#39;])) 
            return false;
        $hasParseFun = false;
        foreach(self::$parseHost as $host) {
            if(strpos ($refererArr[&#39;host&#39;],$host) !== false) { 
                $hasParseFun = true;
                break;
            }   
        }
        if(!$hasParseFun)
            return false;
        $queryVars = array();
        parse_str($refererArr[&#39;query&#39;], $queryVars);
        //调用每个搜索引擎的单独处理方法
        $method = &#39;parse&#39;.ucfirst($host);
        return self::$method($queryVars);
    }
    /* 已取消使用正则的方式。
    public static function buldPattern($rule) {
        $host = str_replace(&#39;.&#39;, &#39;.&#39;, $rule[&#39;host&#39;]);
        $patterns = array();
        foreach ($rule[&#39;key&#39;] as $key) {
            $patterns[] = $host . &#39;.+?&#39;.$key.&#39;=&#39;;
        } 
        return "/(?:" . implode(&#39;|&#39;, $patterns).&#39;)([^&]*)/&#39;;
    }*/
    public static function inBanUrl($referer) {
        foreach(self::$banUrl as $url) {
            if(strpos($url, $referer) !== false) 
                return true;
        }
        return false;
    }
    public static function parseBaidu($params)
    {
        $searchTerms = &#39;&#39;;
        if(isset($params[&#39;kw&#39;])) {
            $searchTerms = $params[&#39;kw&#39;];
        } else if(isset ($params[&#39;wd&#39;])) {
            $searchTerms = $params[&#39;wd&#39;];
        } else if(isset ($params[&#39;word&#39;])) {
            $searchTerms = $params[&#39;word&#39;];
        }
        return isset($params[&#39;ie&#39;]) && (strtolower($params[&#39;ie&#39;]) == &#39;utf-8&#39;) ? $searchTerms : iconv(&#39;gbk&#39;, &#39;utf-8&#39;, $searchTerms);
    }
    public static function parseGoogle($params)
    {
        $searchTerms = &#39;&#39;;
        if(isset($params[&#39;q&#39;])) {
            $searchTerms = $params[&#39;q&#39;];
        }
        return isset($params[&#39;ie&#39;]) && ($params[&#39;ie&#39;] == "GB") ? iconv(&#39;gbk&#39;, &#39;utf-8&#39;, $searchTerms) : $params[&#39;q&#39;];
    }
    public static function parse360($params)
    {
        $searchTerms = &#39;&#39;;
        if(isset($params[&#39;q&#39;])) {
            $searchTerms = $params[&#39;q&#39;];
        }
        return $searchTerms;
    }
    public static function parseSoso($params)
    {
        $searchTerms = &#39;&#39;;
        if(isset($params[&#39;w&#39;])) {
            $searchTerms = $params[&#39;w&#39;];
        }
        return iconv(&#39;gbk&#39;, &#39;utf-8&#39;, $searchTerms);
    }
}


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools