Use PHP to implement mutual transfer of short domain names. Copy the code as follows: *** generate nbsp for a short domain name; * classBuild_URL {private $ mem; private $ base_urlxxx.com; publicfunction _ construct () {$ mem_confarray (
The code is as follows:
/**
* Short domain name generation and resolution
*/
Class Build_URL {
Private $ mem;
Private $ base_url = 'http: // xxx.com /';
Public function _ construct (){
$ Mem_conf = array (
Array (
'Host' => '2014. 168.10.90 ',
'Port' => '123'
),
Array (
'Host' => '2014. 168.10.90 ',
'Port' => '123'
),
);
$ 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;
}
$ Md5 = md5 ($ url );
$ Aid = $ this-> mem-> get ($ md5 );
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 ($ md5, $ 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( "0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 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 ";
}
?>
The http://www.bkjia.com/PHPjc/328002.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/328002.htmlTechArticle code is as follows:/*** short domain name generation nbsp; */class Build_URL {private $ mem; private $ base_url = 'http: // xxx.com/'; public function _ construct () {$ mem_conf = array (...