Using PHP to realize short domain name Mutual transfer

Source: Internet
Author: User
Tags foreach pow sort trim
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 ";


 }


?>

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.