Objective
Short web site services, may be a lot of friends are no longer unfamiliar, and now most of the microblogging, mobile mail reminders and other places have a lot of application mode, and occupy a certain market. It is estimated that many friends are using it now.
Benefits of Short Links:
1, the content needs;
2, user friendly;
3, easy to manage.
The following is a PHP implementation of a short URL conversion algorithm, the code is as follows:
Php
<?php//Short URL generation algorithm class Shorturl {//Character chart public static $charset = 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghij
KLMNOPQRSTUVWXYZ ";
public static function encode ($url) {$key = ' abc ';//Add salt $urlhash = MD5 ($key. $url);
$len = strlen ($urlhash); The encrypted string is divided into 4 segments, 4 bytes per paragraph, each paragraph is calculated, a total of four sets of short connection for ($i = 0; $i < 4; $i + +) {$urlhash _piece = substr ($urlhash, $i * $l
EN/4, $len/4); To do bits and 0x3fffffff bit by bit with, 0X3FFFFFFF represents the binary number of 30 1, that is, 30-bit after the cipher string to zero//here need to use the Hexdec () to convert the 16 into the 10 numeric value type, otherwise the operation will not be normal $hex = he
Xdec ($urlhash _piece) & 0x3fffffff;
The domain name fills in according to the demand $short _url = "http://t.cn/"; Generates a 6-bit short URL for ($j = 0; $j < 6; $j + +) {//The resulting value and 0x0000003d,3d 61, that is, the maximum coordinate value of the CharSet $short _ur
L. = self:: $charset [$hex & 0x0000003d];
After the loop, move the hex to the right 5-bit $hex = $hex >> 5;
} $short _url_list[] = $short _url;
return $short _url_list; }} $url = "http://www.sunbloger.com/";
$short = Shorturl::encode ($url); Print_r ($short);?>
Usually we use the first group of four URLs.
Here need to note that this algorithm is irreversible, therefore, the usual approach is to the short URL and the corresponding original URL into the database, when access, from the database to remove the original URL, through 301 or header to jump.
Summarize
The above is the entire content of this article, I hope that the study or work to bring some help, if you have questions you can message exchange.