当前位置:首页> PHP教程> PHP精通
关键字
文章内容
页面中嵌入google的pagerank显示
 
 
修改时间:[2008/08/11 01:18]    阅读次数:[875]    发表者:[起缘]
 

代码如下:

  1. <?php
  2. /**
  3. * blog.hd001.com Google PageRank Generator
  4. *
  5. *
  6. * @link:    http://blog.hd001.com
  7. * @author:  Pasio & superspice <superspice at yeah dot net>
  8. * @version: 0.1
  9. */
  10. @error_reporting(E_ALL ^ E_NOTICE);
  11. define('GOOGLE_MAGIC', 0xE6359A60);
  12. //unsigned shift right
  13. function zeroFill($a, $b)
  14. {
  15.     $z = hexdec(80000000);
  16.         if ($z & $a)
  17.         {
  18.             $a = ($a>>1);
  19.             $a &= (~$z);
  20.             $a |= 0x40000000;
  21.             $a = ($a>>($b-1));
  22.         }
  23.         else
  24.         {
  25.             $a = ($a>>$b);
  26.         }
  27.         return $a;
  28. }   
  29.    
  30.    
  31. function mix($a,$b,$c) {
  32.   $a -= $b; $a -= $c; $a ^= (zeroFill($c,13));   
  33.   $b -= $c; $b -= $a; $b ^= ($a<<8);   
  34.   $c -= $a; $c -= $b; $c ^= (zeroFill($b,13));
  35.   $a -= $b; $a -= $c; $a ^= (zeroFill($c,12));
  36.   $b -= $c; $b -= $a; $b ^= ($a<<16);
  37.   $c -= $a; $c -= $b; $c ^= (zeroFill($b,5));   
  38.   $a -= $b; $a -= $c; $a ^= (zeroFill($c,3));    
  39.   $b -= $c; $b -= $a; $b ^= ($a<<10);   
  40.   $c -= $a; $c -= $b; $c ^= (zeroFill($b,15));
  41.     
  42.   return array($a,$b,$c);
  43. }
  44.    
  45. function GoogleCH($url, $length=null, $init=GOOGLE_MAGIC) {
  46.     if(is_null($length)) {
  47.         $length = sizeof($url);
  48.     }
  49.     $a = $b = 0x9E3779B9;
  50.     $c = $init;
  51.     $k = 0;
  52.     $len = $length;
  53.     while($len >= 12) {
  54.         $a += ($url[$k+0] +($url[$k+1]<<8) +($url[$k+2]<<16) +($url[$k+3]<<24));
  55.         $b += ($url[$k+4] +($url[$k+5]<<8) +($url[$k+6]<<16) +($url[$k+7]<<24));
  56.         $c += ($url[$k+8] +($url[$k+9]<<8) +($url[$k+10]<<16)+($url[$k+11]<<24));
  57.         $mix = mix($a,$b,$c);
  58.         $a = $mix[0]; $b = $mix[1]; $c = $mix[2];
  59.         $k += 12;   
  60.         $len -= 12;
  61.     }
  62.    
  63.     $c += $length;
  64.     switch($len)              /* all the case statements fall through */
  65.     {
  66.         case 11: $c+=($url[$k+10]<<24);
  67.         case 10: $c+=($url[$k+9]<<16);
  68.         case 9 : $c+=($url[$k+8]<<8);
  69.           /* the first byte of c is reserved for the length */
  70.         case 8 : $b+=($url[$k+7]<<24);
  71.         case 7 : $b+=($url[$k+6]<<16);
  72.         case 6 : $b+=($url[$k+5]<<8);
  73.         case 5 : $b+=($url[$k+4]);
  74.         case 4 : $a+=($url[$k+3]<<24);
  75.         case 3 : $a+=($url[$k+2]<<16);
  76.         case 2 : $a+=($url[$k+1]<<8);
  77.         case 1 : $a+=($url[$k+0]);
  78.          /* case 0: nothing left to add */
  79.     }
  80.     $mix = mix($a,$b,$c);
  81.     /*-------------------------------------------- report the result */
  82.     return $mix[2];
  83. }
  84.    
  85. //converts a string into an array of integers containing the numeric value of the char
  86. function strord($string) {
  87.     for($i=0;$i<strlen($string);$i++) {
  88.         $result[$i] = ord($string{$i});
  89.     }
  90.     return $result;
  91. }
  92. // http://www.example.com/ - Checksum: 6540747202
  93. if (!isset($_GET['url']))
  94. {
  95.     $url_o   = $_SERVER['HTTP_REFERER'];
  96. }
  97. else
  98. {
  99.     $url_o   = $_GET['url'];
  100. }
  101. $url_o = explode("?", $url_o);
  102. $url_o = $url_o[0];
  103. $url    = 'info:'.$url_o;
  104. $ch     = GoogleCH(strord($url));
  105. $url    ='info:'.urlencode($url_o);
  106. $cache_filename = "prcache/".urlencode($url_o).".pagerank";
  107. // check exists of cache file, if not exists, create it
  108. if(!file_exists($cache_filename) || (file_exists($cache_filename) && filemtime($cache_filename) < time() - 86400))
  109. {
  110.     $url    = "http://toolbarqueries.google.com/search?client=navclient-auto&ch=6$ch&ie=UTF-8&oe=UTF-8&features=Rank&q=$url";
  111.     $result = @file_get_contents($url);
  112.     $tmp    = @explode(":", $result);
  113.     $rank   = intval($tmp[2]);
  114.     $handle = fopen ($cache_filename, "w");
  115.     flock($handle, LOCK_EX);
  116.     fwrite($handle, $rank);
  117.     flock($handle, LOCK_UN);
  118.     fclose($handle);
  119. }
  120. $pagerank = @file_get_contents($cache_filename);
  121. $pagerank = 2;
  122. $width = 6 * $pagerank;
  123. $width_ = 6 * (10-$pagerank);
  124. echo "document.writeln('<table width=\"80\" border=0 cellpadding=\"0\" cellspacing=\"0\" style=\"font-size:12px\"><tr><td align=\"center\">PageRank</td></tr><tr><td><table width=\"80\" cellpadding=\"0\" cellspacing=\"1\" bgcolor=\"#CCCCCC\" height=\"10\" style=\"font-size:11px\"><tr bgcolor=\"#FFFFFF\"><td width=\"62\"><img src=\"/imgs/bar.gif\" width=\"$width\" height=\"20\"><img src=\"/imgs/space.gif\" width=\"$width_\" height=\"20\"></td><td align=\"center\" style=\"color:red\">$pagerank</td></tr></table></td></tr></table>');";

以上代码保存为pagerank.php到plugins目录下。
实现步骤
一、放一个bar.gif到imgs文件夹下,为上图中的绿色图片,再放一个space.gif到imgs文件夹下,该图片是一个1*1的空白图。
二、在plugins目录下建立prcache目录,并保证该目录为apache执行用户可读写的。
三、调用方法是:
页面中,在你想要显示pagerank的地方加入如下代码
<script language="javascript" src="plugins/pagerank.php"></script>
完毕!