Php ideas and steps for generating short URLs (recommended)

Source: Internet
Author: User
The idea and Method (recommended) for php to generate short URLs and the implementation of using php to generate short URLs. The idea of generating a short URL: If you have restored the short URL, do you know what it looks like? For example: www.jbxue.comsitejs-17300-1.html for the above link, in addition to open directly, there is also a way to open it, as follows: www. jbxue. comli php to generate short Web site Ideas and Methods (recommended)

The idea of generating short URLs and the implementation of using php to generate short URLs.

The idea of generating a short URL: If you have restored the short URL, do you know what it looks like? For example:
Http://www.jbxue.com/sitejs-17300-1.html
In addition to directly opening the above link, there is also a way to open it, as shown below:
Http: // www. jbxue.com/link.php? Url = http://www.jbxue.com/sitejs-17300-1.html
Now, the short website actually looks like this. you may see that the short website in the Sina Weibo application looks like this:
Http://t.cn/zHEYrvV
In fact, it may look like this:
Http://t.cn/link.php? Url = http://www.jbxue.com/sitejs-17300-1.html
Now, let's talk about step 2.
Http://t.cn/link.php? Url = http://www.jbxue.com/sitejs-17300-1.html
Scale down
Http://t.cn/zHEYrvV
In this case, url rewriting is required. In this example, you can rewrite it as follows:
RewriteEngine On?
RewriteBase /?
RewriteRule ^/(. *) $ link. php? Url = $1 [L]?
Here is the implementation of the http://t.cn/link.php? Url = zHEYrvV conversion for http://t.cn/zHEYrvV, shorten a lot, then how to through zHEYrvV to find the http://www.jbxue.com/sitejs-17300-1.html this url and jump to this url? Here we use an encryption-like algorithm to shorten all the long URLs into a corresponding string of 5-6 characters, and store the corresponding relationship in the database. In combination with this example, you can find the corresponding URL in the database based on the input parameter zHEYrvV. if you find the URL, the header will jump over.
OK. the idea of generating short URLs is like this.

The process of generating a short URL using php (the long URL must be 5-6 characters in length and must be unique ):

 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; } // www.jbxue.comfunction shorturl($url){ $url=crc32($url); $result=sprintf("%u",$url); return code62($result); } 

For example

Echo response url ('http: // www.jbxue.com /');?
The generated unique correspondence code is n2Q8e.

References:

  • Example code of generating a short URL using php
  • Ideas and implementation of generating short URLs in php
  • Php generates code for a short URL, similar to a microblog short URL
  • Php Weibo short URL algorithm php code for generating short URLs
  • Php short URL super simple code
  • Php simple code for generating short URLs
  • Php code for generating short URLs
  • Php short link, short url, short url implementation code
  • Php short URL implementation code

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.