How can I use PHP to implement mutual transfer of short domain names? The following code can help you implement it. It is very simple. For more information, see
How can I use PHP to implement mutual transfer of short domain names? The following code can help you implement it. It is very simple. For more information, see
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 ("# ^ [| 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 ";
}
?>
, Website space, U.S. servers, Hong Kong servers