PHP to convert URL to a short URL algorithm to share _php instance

Source: Internet
Author: User
Tags numeric value

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.

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.