在帝国cms中使用短网址,有利于搜索引擎的优化,增加网站内容被采集的难度。其核心为下面的编码和解码函数:
<?php
function wangzhi_encode($classid,$id){
global $class_r;
$tid=$class_r[$classid]['tid'];
$mum=strlen($tid);
$in=$mum.$tid.$id;
$dict=array_merge(range('A','Z'),range('a','z'),range(0,9));
$d=count($dict);
$re='';
do{
$y=$in%$d;
$re=$dict[$y].$re;
$in=floor($in/$d);
}while($in>0);
return $re;
}
function wangzhi_decode($str){
global $class_r,$etable_r,$empire,$dbtbpre;
$dict=array_merge(range('A','Z'),range('a','z'),range(0,9));
$d=count($dict);
$re=0;
while($str){
$re=$re*$d+array_search(substr($str,0,1),$dict);
$str=substr($str,1);
}
$flag=(int)substr($re,0,1);
$code[tid]=substr($re,1,$flag);
$code[id]=substr($re,$flag+1);
foreach($etable_r as $tbname=>$v)if($you=$v[mid]==$code[tid])break;
if(!$you)$tbname='';
$code[tbname]=$tbname;
if($tbname)$code[classid]=$empire->gettotal("select classid as total from {$dbtbpre}ecms_{$tbname}_index where id=$code[id]");
return $code;
}
print_r(wangzhi_decode('JEW4'));
?>
实则,运行
echo wangzhi_encode(896,124381);
显示为JEW4
运行
wangzhi_decode('JEW4');
返回数组为:
Array
(
[tid] => 1
[id] => 124381
[tbname] => news
[classid] => 896
)
|