首頁 > php教程 > php手册 > 主體

PHP记录用户通过搜索引擎进网站的关键词并给出相关结果

WBOY
發布: 2016-05-25 16:43:59
原創
1333 人瀏覽過

本文章给大家介绍PHP记录用户通过搜索引擎进网站的关键词并给出相关结果,有需要了解的同学可进入参考,下面是进入你网站的情况的相关实现代码,更多的,大家自己拓展,代码如下:

<?php
$rfr = $_SERVER[&#39;HTTP_REFERER&#39;];
//if(!$rfr) $rfr=&#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;];
if ($rfr) {
    $p = parse_url($rfr);
    parse_str($p[&#39;query&#39;], $pa);
    $p[&#39;host&#39;] = strtolower($p[&#39;host&#39;]);
    $arr_sd_key = array(
        &#39;baidu.com&#39; => &#39;word&#39;,
        &#39;google.com&#39; => &#39;q&#39;,
        &#39;sina.com.cn&#39; => &#39;word&#39;,
        &#39;sohu.com&#39; => &#39;word&#39;,
        &#39;msn.com&#39; => &#39;q&#39;,
        &#39;bing.com&#39; => &#39;q&#39;,
        &#39;163.com&#39; => &#39;q&#39;,
        &#39;yahoo.com&#39; => &#39;p&#39;
    );
    $keyword = &#39;&#39;;
    $sengine = $p[&#39;host&#39;];
    foreach ($arr_sd_key as $se => $kwd) {
        if (strpos($p[&#39;host&#39;], $se) !== false) {
            $keyword = $pa[$kwd];
            $sengine = $se;
            break;
        }
    }
    $sql = "insert into visit_log(domain,key_word,ct)";
}
?>
上面的代码实现不了根据用户来的词给出相关结果,下面我来再进行调上面的函数,代码如下:
<?php
function unescape($str) {
    $ret = &#39;&#39;;
    $len = strlen($str);
    for ($i = 0; $i < $len; $i++) {
        if ($str[$i] == &#39;%&#39; && $str[$i + 1] == &#39;u&#39;) {
            $val = hexdec(substr($str, $i + 2, 4));
            if ($val < 0x7f) $ret.= chr($val);
            else if ($val < 0x800) $ret.= chr(0xc0 | ($val >> 6)) . chr(0x80 | ($val & 0x3f));
            else $ret.= chr(0xe0 | ($val >> 12)) . chr(0x80 | (($val >> 6) & 0x3f)) . chr(0x80 | ($val & 0x3f));
            $i+= 5;
        } else if ($str[$i] == &#39;%&#39;) {
            $ret.= urldecode(substr($str, $i, 3));
            $i+= 2;
        } else $ret.= $str[$i];
    }
    return $ret;
}
function ls_get_delim($ref) {
    $search_engines = array(
        &#39;google.com.hk&#39; => &#39;q&#39;,
        &#39;google.com.tw&#39; => &#39;q&#39;,
        &#39;go.google.com&#39; => &#39;q&#39;,
        &#39;google.com&#39; => &#39;q&#39;,
        &#39;blogsearch.google.com&#39; => &#39;q&#39;,
        &#39;cn.bing.com&#39; => &#39;q&#39;,
        &#39;one.cn.yahoo.com&#39; => &#39;p&#39;,
        &#39;baidu.com&#39; => &#39;wd&#39;,
        &#39;soso.com&#39; => &#39;w&#39;,
        &#39;youdao.com&#39; => &#39;q&#39;,
        &#39;sogou.com&#39; => &#39;query&#39;
    );
    $delim = false;
    // 判断
    if (isset($search_engines[$ref])) {
        $delim = $search_engines[$ref];
    }
    return $delim;
}
function ls_get_refer() {
    // 判断前一页面的 URL 地址
    $queryString = $_GET[&#39;referer&#39;];
    $queryString = unescape($queryString);
    if (!isset($queryString) || ($queryString == &#39;&#39;)) return false;
    $referer_info = parse_url($queryString);
    $referer = $referer_info[&#39;host&#39;];
    //去除www.
    if (substr($referer, 0, 4) == &#39;www.&#39;) $referer = substr($referer, 4);
    return $referer;
}
function ls_getinfo($what) {
    $referer = ls_get_refer();
    if (!$referer) return false;
    $delimiter = ls_get_delim($referer);
    if ($delimiter) {
        $terms = ls_get_terms($delimiter);
        if ($what == &#39;isref&#39; && $terms != &#39;&#39;) {
            return true;
        }
        if ($what == &#39;terms&#39;) {
            echo $terms;
        }
    }
    return false;
}
function yxiao_seems_utf8($str) {
    $length = strlen($str);
    for ($i = 0; $i < $length; $i++) {
        $c = ord($str[$i]);
        if ($c < 0x80) $n = 0; // 0bbbbbbb
        elseif (($c & 0xE0) == 0xC0) $n = 1; // 110bbbbb
        elseif (($c & 0xF0) == 0xE0) $n = 2; // 1110bbbb
        elseif (($c & 0xF8) == 0xF0) $n = 3; // 11110bbb
        elseif (($c & 0xFC) == 0xF8) $n = 4; // 111110bb
        elseif (($c & 0xFE) == 0xFC) $n = 5; // 1111110b
        else return false; // Does not match any model
        for ($j = 0; $j < $n; $j++) { // n bytes matching 10bbbbbb follow ?
            if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80)) return false;
        }
    }
    return true;
}
function ls_get_terms($d) {
    //取得查询值
    $queryString = $_GET[&#39;referer&#39;];
    $queryString = unescape($queryString);
    $query_str = parse_url($queryString);
    parse_str($query_str[query], $query_str);
    $query = $query_str[$d];
    $query = urldecode($query);
    $query = str_replace("&#39;", &#39;&#39;, $query);
    $query = str_replace(&#39;"&#39;, &#39;&#39;, $query);
    $query_array = preg_split(&#39;/[s,+.]+/&#39;, $query);
    $query_terms = implode(&#39; &#39;, $query_array);
    $terms = htmlspecialchars($query_terms);
    //gbk->utf8
    if (!yxiao_seems_utf8($terms)) {
        $terms = iconv("GBK", "UTF-8//IGNORE", $terms);
    }
    return $terms;
}
if (ls_getinfo(&#39;isref&#39;)) {
    document . write(&#39;<div id="serp"><p>&#39;) document . write(&#39;更多搜索结果:<em><a href="/search?q=<?php ls_getinfo(terms); " &#39;) document . write(&#39;title="查看<?php ls_getinfo(terms); 的搜索结果" rel="nofollow"><?php ls_getinfo(terms); </a></em>&#39;) document . write(&#39;</p></div>&#39;); < ? php
}
登入後複製


本文地址:

转载随意,但请附上文章地址:-)

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板