当前位置:  编程技术>php
本页文章导读:
    ▪Google PR值的PHP实现代码      将以下代码保存为googlepr.php,读取www.网站的PR值调用格式为: googlepr.php?url=www. 例子:   代码示例: <?php //PageRank Lookup v1.1 by HM2K (update: 31/01/07) //based on an alogoritham found here: http://pagerank.gam.........
    ▪PHP查询网站PR值的实现代码      说明: PR值是google衡量网站的重要标准之一。 本节分享一个用PHP查询网站的PR值的方法。   根据google提供的结果获取pr值,如: http://toolbarqueries.google.com.hk/tbr?client=navclient-auto&features=Rank.........
    ▪PHP禁止IP访问网站的实例代码      本节内容: PHP禁止个别IP访问自己的网站   例子,不让某个IP访问网站,可以封掉其IP地址。 如下:   代码示例: <?php function get_ip_data(){       $ip=file_get_contents("http://ip.taobao.com/servi.........

[1]Google PR值的PHP实现代码
    来源: 互联网  发布时间: 2013-12-24

将以下代码保存为googlepr.php,读取www.网站的PR值调用格式为:
googlepr.php?url=www.

例子:
 

代码示例:
<?php
//PageRank Lookup v1.1 by HM2K (update: 31/01/07)
//based on an alogoritham found here: http://pagerank.gamesaga.net/
 
//settings - host and user agent
$googlehost='toolbarqueries.google.com';
$googleua='Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5';
 
//字符串转换到32位整数
function StrToNum($Str, $Check, $Magic) {
    $Int32Unit = 4294967296;  // 2^32
 
    $length = strlen($Str);
    for ($i = 0; $i < $length; $i++) {
        $Check *= $Magic;  
        //If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31),
        //  the result of converting to integer is undefined
        //  refer to http://www.php.net/manual/en/language.types.integer.php
        if ($Check >= $Int32Unit) {
            $Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
            //if the check less than -2^31
            $Check = ($Check < -2147483648) ? ($Check + $Int32Unit) : $Check;
        }
        $Check += ord($Str{$i});
    }
    return $Check;
}
 
//将URL进行哈希编码
function HashURL(/blog_article/$String/index.html) {
    $Check1 = StrToNum($String, 0x1505, 0x21);
    $Check2 = StrToNum($String, 0, 0x1003F);
 
    $Check1 >>= 2;   
    $Check1 = (($Check1 >> 4) & 0x3FFFFC0 ) | ($Check1 & 0x3F);
    $Check1 = (($Check1 >> 4) & 0x3FFC00 ) | ($Check1 & 0x3FF);
    $Check1 = (($Check1 >> 4) & 0x3C000 ) | ($Check1 & 0x3FFF);  
    
    $T1 = (((($Check1 & 0x3C0) << 4) | ($Check1 & 0x3C)) <<2 ) | ($Check2 & 0xF0F );
    $T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0x3C00)) << 0xA) | ($Check2 & 0xF0F0000 );
    
    return ($T1 | $T2);
}
 
//为哈希字符串生成校验码
function CheckHash($Hashnum) {
    $CheckByte = 0;
    $Flag = 0;
 
    $HashStr = sprintf()('%u', $Hashnum) ;
    $length = strlen($HashStr);
    
    for ($i = $length - 1;  $i >= 0;  $i --) {
        $Re = $HashStr{$i};
        if (1 === ($Flag % 2)) {             
            $Re += $Re;    
            $Re = (int)($Re / 10) + ($Re % 10);
        }
        $CheckByte += $Re;
        $Flag ++;  
    }
 
    $CheckByte %= 10;
    if (0 !== $CheckByte) {
        $CheckByte = 10 - $CheckByte;
        if (1 === ($Flag % 2) ) {
            if (1 === ($CheckByte % 2)) {
                $CheckByte += 9;
            }
            $CheckByte >>= 1;
        }
    }
 
    return '7'.$CheckByte.$HashStr;
}
 
//返回pagerank哈希校验码
function getch($url) { return CheckHash(HashURL(/blog_article/$url/index.html)); }
 
//返回PR值
function getpr($url) {
    global $googlehost,$googleua;
    $ch = getch($url);
    $fp = fsockopen($googlehost, 80, $errno, $errstr, 30);
    if ($fp) {
       $out = "GET /search?client=navclient-auto&ch=$ch&features=Rank&q=info:$url HTTP/1.1\r\n";
       //echo "<pre>$out</pre>\n"; //debug only
       $out .= "User-Agent: $googleua\r\n";
       $out .= "Host: $googlehost\r\n";
       $out .= "Connection: Close\r\n\r\n";
    
       fwrite($fp, $out);
       
       //$pagerank = substr(fgets($fp, 128), 4); //debug only
       //echo $pagerank; //debug only
       while (!feof($fp)) {
            $data = fgets($fp, 128);
            //echo $data;
            $pos = strpos($data, "Rank_");
            if($pos === false){} else{
                $pr=substr($data, $pos + 9);
                $pr=trim($pr);
                $pr=str_replace()("\n",'',$pr);
                return $pr;
            }
       }
       //else { echo "$errstr ($errno)<br />\n"; } //debug only
       fclose($fp);
    }
}
 
//生成pagerank图形
function pagerank($url,$width=40,$method='style') {
    if (!preg_match('/^(http:\/\/)?([^\/]+)/i', $url)) { $url='http://'.$url; }
    $pr=getpr($url);
    $pagerank="PageRank: $pr/10";
 
    //The (old) image method
    if ($method == 'image') {
    $prpos=$width*$pr/10;
    $prneg=$width-$prpos;
    $html='<img src="/images/pos.gif" width='.$prpos.' height=4 border=0 alt="'.$pagerank.'"><img src="/images/neg.gif" width='.$prneg.' height=4 border=0 alt="'.$pagerank.'">';
    }
    //The pre-styled method
    if ($method == 'style') {
    $prpercent=100*$pr/10;
    $html='<div .$width.'px; padding: 0; background: #D9D9D9;"><strong .$prpercent.'%; display: block; position: relative; background: #5EAA5E; text-align: center; color: #333; height: 4px; line-height: 4px;"><span></span></strong></div>';
    }
    
    $out='<a href="'.$url.'" title="'.$pagerank.'">'.$html.'</a>';
    return $out;
}
 
if ((!isset()($_POST['url'])) && (!isset($_GET['url']))) { echo '<form action="" method="post"><input name="url" type="text"><input type="submit" name="Submit" value="Get Pagerank"></form>'; }
if (isset($_REQUEST['url'])) { echo pagerank($_REQUEST['url']); }
?>

    
[2]PHP查询网站PR值的实现代码
    来源: 互联网  发布时间: 2013-12-24

说明:
PR值是google衡量网站的重要标准之一。
本节分享一个用PHP查询网站的PR值的方法。

 
根据google提供的结果获取pr值,如:
http://toolbarqueries.google.com.hk/tbr?client=navclient-auto&features=Rank:&q=info:phpddt.com&ch=8fabc62ea

例子:
 

代码示例:
<?php
/*
* 功能:对URL进行编码
* 参数说明:$web_url 网站URL,不包含"http://"
* site: www.
*/
function HashURL(/blog_article/$url/index.html)
{  
    $SEED = "Mining PageRank is AGAINST GOOGLE'S TERMS OF SERVICE. Yes, I'm talking to you, scammer.";
    $Result = 0x01020345;
    for ($i=0; $i<strlen($url); $i++)
    {
        $Result ^= ord($SEED{$i%87}) ^ ord($url{$i});
        $Result = (($Result >> 23) & 0x1FF) | $Result << 9;
    }
    return sprintf()("8%x", $Result);
}
/*
*功能:根据google提供的pr查询接口获取pagerank
*参数说明:$domain 网站域名,不包含"http://"
*/
function pagerank($domain)
{   
    $StartURL = "http://toolbarqueries.google.com/tbr?client=navclient-auto&features=Rank:&q=info:";
    $GoogleURL = $StartURL.$domain. '&ch='.HashURL(/blog_article/$domain/index.html);
    echo $GoogleURL.'<br>';
    $fcontents = file_get_contents("$GoogleURL");
    $pagerank = substr($fcontents,9);
    if (!$pagerank) return "0";else return $pagerank;
}
echo pagerank("phpddt.com");
?>

    
[3]PHP禁止IP访问网站的实例代码
    来源: 互联网  发布时间: 2013-12-24

本节内容:
PHP禁止个别IP访问自己的网站
 
例子,不让某个IP访问网站,可以封掉其IP地址。
如下:
 

代码示例:
<?php
function get_ip_data(){  
    $ip=file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=".get_client_ip());
    $ip = json_decode()($ip);
    if($ip->code){
        return false;
    }
    $data = (array) $ip->data;
    if($data['region']=='湖北省' && !isCrawler()){
        exit('http://www.a.net');
    }
}
function isCrawler() {
        $spiderSite= array(
                        "TencentTraveler",
                        "Baiduspider+",
                        "BaiduGame",
                        "Googlebot",
                        "msnbot",
                        "Sosospider+",
                        "Sogou web spider",
                        "ia_archiver",
                        "Yahoo! Slurp",
                        "YoudaoBot",
                        "Yahoo Slurp",
                        "MSNBot",
                        "Java (Often spam bot)",
                        "BaiDuSpider",
                        "Voila",
                        "Yandex bot",
                        "BSpider",
                        "twiceler",
                        "Sogou Spider",
                        "Speedy Spider",
                        "Google AdSense",
                        "Heritrix",
                        "Python-urllib",
                        "Alexa (IA Archiver)",
                        "Ask",
                        "Exabot",
                        "Custo",
                        "OutfoxBot/YodaoBot",
                        "yacy",
                        "SurveyBot",
                        "legs",
                        "lwp-trivial",
                        "Nutch",
                        "StackRambler",
                        "The web archive (IA Archiver)",
                        "Perl tool",
                        "MJ12bot",
                        "Netcraft",
                        "MSIECrawler",
                        "WGet tools",
                        "larbin",
                        "Fish search",
                );
        if(in_array(strtolower()($_SERVER['HTTP_USER_AGENT']),$spiderSite)){
            return true;
        }else{
            return false;
        }
}
//取客户端 ip
function get_client_ip()
{
    if (isset()($_SERVER)){
            if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
                $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
            } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
                $realip = $_SERVER["HTTP_CLIENT_IP"];
            } else {
                $realip = $_SERVER["REMOTE_ADDR"];
            }
    } else {
            if (getenv("HTTP_X_FORWARDED_FOR")){
                $realip = getenv("HTTP_X_FORWARDED_FOR");
            } else if (getenv("HTTP_CLIENT_IP")) {
                $realip = getenv("HTTP_CLIENT_IP");
            } else {
                $realip = getenv("REMOTE_ADDR");
            }
        }
    return $realip;
}

您可能感兴趣的文章:
php 禁止单个IP地址或IP段访问的实现代码
php禁止ip访问的函数

    
最新技术文章:
▪PHP函数microtime()时间戳的定义与用法
▪PHP单一入口之apache配置内容
▪PHP数组排序方法总结(收藏)
▪php数组排序方法大全(脚本学堂整理奉献)
▪php数组排序的几个函数(附实例)
▪php二维数组排序(实例)
▪php根据键值对二维数组排序的小例子
▪php验证码(附截图)
▪php数组长度的获取方法(三个实例)
▪php获取数组长度的方法举例
▪判断php数组维度(php数组长度)的方法
▪php获取图片的exif信息的示例代码
▪PHP 数组key长度对性能的影响实例分析
▪php函数指定默认值的方法示例
▪php提交表单到当前页面、提交表单后页面重定...
▪php四舍五入的三种实现方法
▪php获得数组长度(元素个数)的方法
▪php日期函数的简单示例代码
▪php数学函数的简单示例代码
▪php字符串函数的简单示例代码
▪php文件下载代码(多浏览器兼容、支持中文文...
▪php实现文件下载、支持中文文件名的示例代码...
▪php文件下载(防止中文文件名乱码)的示例代码
IT科技资讯 iis7站长之家
▪php数组去重(一维、二维数组去重)的简单示例
▪php小数点后取两位的三种实现方法
▪php Redis 队列服务的简单示例
▪PHP导出excel时数字变为科学计数的解决方法
▪PHP数组根据值获取Key的简单示例
▪php数组去重的函数代码示例
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3