PHP Generate short URL Implementation program code _php tutorial

Source: Internet
Author: User
Generate short URLs we can directly use a function to generate a unique 5-6-character address, but we also need to do a bit of direct use for static jump, let me introduce to you.

Generate a short URL program

The code is as follows Copy Code

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);
}

This generates a job like HTT:/Z.CN/ABCFC, but we need to implement a pseudo-static that can be accessed on your Apache or IIS configuration, as follows

The code is as follows Copy Code

How to add

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

Shrink into

Http://z.cn/zHEYrvV

This place requires a URL rewrite, which, in this case, 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.html www.bkjia.com true http://www.bkjia.com/PHPjc/631552.html techarticle generate short URLs we can directly use a function to generate a unique 5-6-character address, but we also need to do a bit of direct use for static jump, below I ...

  • Related Article

    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.