An example of an algorithm for implementing URL transformation short URLs in PHP

Source: Internet
Author: User
Tags md5 numeric value

Short URL, as the name implies is in the form of a relatively short URL. In Web 2.0 today, it has to be said that this is a trend. There are already many similar services, with short URLs you can use a short URL to replace a lengthy web site, allowing users to share links more easily.

Algorithm principle

1) will be long Web site MD5 generated 32-bit signature string, divided into 4 paragraphs, 8 bytes per paragraph;
2 to the four-segment loop, take 8 bytes, consider him as 16-string and 0X3FFFFFFF (30-bit 1) and operation, that is, more than 30-bit ignore processing;
3 The 30 bits are divided into 6 segments, each 5 digit number is taken as the index of the alphabet to obtain the specific characters, and then the 6-bit string is obtained.
4) The total MD5 string can obtain 4 6-bit strings; Taking any one inside can be used as the short URL of this long URL;

The following is a PHP implementation of a short URL conversion algorithm, the code is as follows:

Php

The code is as follows Copy Code

<?php
Short URL generation algorithm
Class Shorturl {

Character Tabulation
public static $charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";

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 segment is calculated, and a total of four groups of short connections can be generated.
for ($i = 0; $i < 4; $i + +) {
$urlhash _piece = substr ($urlhash, $i * $len/4, $len/4);

To do bit and 0X3FFFFFFF, 0X3FFFFFFF represents the binary number of 30 1, that is, 30-bit after the cipher string is zero
Here you need to use HEXDEC () to convert the 16 string into a 10-numeric value, otherwise the operation will be abnormal
$hex = Hexdec ($urlhash _piece) & 0x3fffffff;

The domain name fills in according to the demand
$short _url = "http://t.cn/";

Generate 6-bit short URLs
for ($j = 0; $j < 6; $j + +) {

The resulting value and 0x0000003d,3d is 61, that is, the charset coordinate maximum value
$short _url. = self:: $charset [$hex & 0x0000003d];

Move the hex to the right 5 digits after the loop is finished
$hex = $hex >> 5;
}

$short _url_list[] = $short _url;
}

return $short _url_list;
}
}

$url = "http://www.111cn.net/";
$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.

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.