Mutual transfer of short domain names using PHP

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.