• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

利用PHP实现短域名互转_php技巧

php 搞代码 3年前 (2022-01-26) 30次浏览 已收录 0个评论
/**<BR> <STRONG> * 短域名生成&解析类<BR></STRONG>  */<BR> class Build_URL {<br><br>     private $mem;<BR>     private $base_url = 'http://xxx.com/';<br><br>     public function  __construct() {<BR>         $mem_conf    = array(<BR>                 array(<BR>                         'host'    => '192.168.10.90',<BR>                         'port'    => '11116'<BR>                 ),<BR>                 array(<BR>                         'host'    => '192.168.10.90',<BR>                         'port'    => '11117'<BR>                 ),<BR>         );<BR>         $this->mem    = new Memcache();<BR>         foreach ($mem_conf as $v) {<BR>             $this->mem->addServer($v['host'], $v['port']);<BR>         }<BR>     }<br><br>     public function encode($url) {<BR>         $url    = trim($url);<BR>         if(!preg_match("#^[http://|https://|ftp://]#iS", $url)) {<BR>             return false;<BR>         }<BR>         $md5    = md5($url);<BR>         $aid    = $this->mem->get($md5);<BR>         if(!$aid) {<BR>             if(($aid = $this->mem->increment('auto_increment_id')) === false) {<BR>                 $this->mem->set('auto_increment_id', 10000);<BR>                 $aid = $this->mem->increment('auto_increment_id');<BR>       <i style="color:transparent">@本文来源gaodai$ma#com搞$代*码6网</i><b>搞代gaodaima码</b>      }<BR>             $this->mem->set($md5, $aid);<BR>             $key    = $this->dec2any($aid);<BR>             $this->mem->set($key, $url);<BR>         } else {<BR>             $key    = $this->dec2any($aid);<BR>         }<br><br>         return $this->base_url.$key;<BR>     }<br><br>     public function decode($url) {<BR>         $key    = str_replace($this->base_url, '', trim($url));<BR>         return $this->mem->get($key);<BR>     }<br><br>     private function dec2any($num, $base=62, $index=false) {<BR>         $out = '';<BR>         if (! $base ) {<BR>             $base = strlen($index);<BR>         } else if (! $index ) {<BR>             $index = substr("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ,0 ,$base);<BR>         }<BR>         $t = ($num == 0) ? 0 : floor(log10($num) / log10($base));<BR>         for ($t; $t >= 0; $t--) {<BR>             $a = floor($num / pow( $base, $t ));<BR>             $out = $out . substr($index, $a, 1);<BR>             $num = $num - ($a * pow( $base, $t ));<BR>         }<BR>         return $out;<BR>     }<BR> }<br><br> $app = new Build_URL();<BR> $url = array(<BR>     'http://www.baidu.com',<BR>     'http://www.google.com',<BR>     'http://www.gaodaima.com'<BR> );<BR> foreach ($url as $v) {<BR>     $sort    = $app->encode($v);<BR>     echo "sort link: ".$sort."\n";<BR>     $original    = $app->decode($sort);<BR>     echo "original: ".$original."\n";<BR> }<BR> ?><BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:利用PHP实现短域名互转_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址