php

位置:IT落伍者 >> php >> 浏览文章

利用PHP实现短域名互转


发布日期:2019年10月25日
 
利用PHP实现短域名互转
如何使用PHP实现短域名互转?下面的代码可以帮助你实现非常简单需要的朋友可以参考下复制代码 代码如下:


/**
* 短域名生成&解析类
*/
class Build_URL {

private $mem;
private $base_url = ;

public function __construct() {
$mem_conf = array(
array(
host =>
port =>
)
array(
host =>
port =>
)
);
$this>mem = new Memcache();
foreach ($mem_conf as $v) {
$this>mem>addServer($v[host] $v[port]);
}
}

public function encode($url) {
$url = trim($url);
if(!preg_match("#^[http://|https://|ftp://]#iS" $url)) {
return false;
}
$md = md($url);
$aid = $this>mem>get($md);
if(!$aid) {
if(($aid = $this>mem>increment(auto_increment_id)) === false) {
$this>mem>set(auto_increment_id );
$aid = $this>mem>increment(auto_increment_id);
}
$this>mem>set($md $aid);
$key = $this>decany($aid);
$this>mem>set($key $url);
} else {
$key = $this>decany($aid);
}

return $this>base_url$key;
}

public function decode($url) {
$key = str_replace($this>base_url trim($url));
return $this>mem>get($key);
}

private function decany($num $base= $index=false) {
$out = ;
if (! $base ) {
$base = strlen($index);
} else if (! $index ) {
$index = substr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" $base);
}
$t = ($num == ) ? : floor(log($num) / log($base));
for ($t; $t >= ; $t) {
$a = floor($num / pow( $base $t ));
$out = $out substr($index $a );
$num = $num ($a * pow( $base $t ));
}
return $out;
}
}

$app = new Build_URL();
$url = array(



);
foreach ($url as $v) {
$sort = $app>encode($v);
echo "sort link: "$sort"n";
$original = $app>decode($sort);
echo "original: "$original"n";
}
?>

               

上一篇:解析php做推送服务端实现ios消息推送

下一篇:php ajax用户注册检测代码