Home >Backend Development >PHP Tutorial >Generate short URL implementation program code in php_PHP tutorial

Generate short URL implementation program code in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:57:101128browse

To generate a short URL, we can directly use a function to generate a unique address with a length of 5-6 characters, but what we need to do is to directly use it to make a static jump. I will introduce it to you below.

Generate short URL program

The job generated in this way is like htt:/z.cn/abcfc, but if we want to achieve access, we need to configure a pseudo-static in your apache or iis, as follows
The code is as follows
 代码如下 复制代码

function code62($x){
$show='';
while($x>0){
  $s=$x % 62;
  if ($s>35){
   $s=chr($s+61);
  }elseif($s>9&&$s<=35){
$s=chr($s+55);
}
$show.=$s;
$x=floor($x/62);
}
return $show;
}
function shorturl($url){
$url=crc32($url);
$result=sprintf("%u",$url);
return code62($result);
}

Copy code

代码如下 复制代码

如何将

http://z.cn/link.php?url=http://www.bKjia.c0m

缩成

http://z.cn/zHEYrvV

function code62($x){
$show='';
while($x>0){
$s=$x % 62;
if ($s>35){
$s=chr($s+61);
}elseif($s>9&&$s<=35){
$s=chr($s+55);
}
$show.=$s;
$x=floor($x/62);
}
return $show;
}
function shorturl($url){
$url=crc32($url);
$result=sprintf("%u",$url);
return code62($result);
}

 代码如下 复制代码

RewriteEngine On 

RewriteBase / 

RewriteRule ^/(.*)$ link.php?url=[L]

The code is as follows Copy code

http://z.cn/link.php?url=http://www.bKjia.c0m Shrunk to http://z.cn/zHEYrvV This place needs to use url rewriting. According to this example, it can be rewritten like this:
The code is as follows Copy code
RewriteEngine On RewriteBase / RewriteRule ^/(.*)$ link.php?url=$1[L] http://www.bkjia.com/PHPjc/631552.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631552.htmlTechArticleGenerate short URL We can directly use a function to generate a unique address with a length of 5-6 characters, but One more thing we need to do is to directly use static jumps. Next I...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn