How to use PHP to achieve a short domain name mutual transfer? The following code can help you realize that very simply, need a friend can refer to the following
copy code code as follows:
/**
* Short domain name generation & Resolution Class
*/
class Build_url {
private $mem;
private $base _url = ' http://xxx.com/';
Public Function __construct () {
$mem _conf = Array (
Array (
' host ' => ' 192.168.10.90 ',
' Port ' => ' 11116 '
),
Array (
' host ' => ' 192.168.10.90 ',
' Port ' => ' 11117 '
),
);
$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 5 = MD5 ($url);
$aid = $this->mem->get ($md 5);
if (! $aid) {
if (($aid = $this->mem->increment (' auto_increment_id ')) = = False) {
$this->mem->set (' auto_increment_id ', 10000);
$aid = $this->mem->increment (' auto_increment_id ');
}
$this->mem->set ($MD 5, $aid);
$key = $this->dec2any ($aid);
$this->mem->set ($key, $url);
} else {
$key = $this->dec2any ($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 Dec2any ($num, $base =62, $index =false) {
$out = ';
if (! $base) {
$base = strlen ($index);
} else if (! $index) {
$index = substr ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $base);
}
$t = ($num = = 0)? 0:floor (log10 ($num)/log10 ($base));
for ($t; $t >= 0; $t-) {
$a = Floor ($num/pow ($base, $t));
$out = $out. substr ($index, $a, 1);
$num = $num-($a * POW ($base, $t));
}
return $out;
}
}
$app = new Build_url ();
$url = Array (
' http://www.baidu.com ',
' http://www.google.com ',
' Http://www.jb51.net '
);
foreach ($url as $v) {
$sort = $app->encode ($v);
echo "Sort link:". $sort. " n ";
$original = $app->decode ($sort);
echo "Original:". $original. " n ";
}
?>