PHP converts a URL to a short URL Algorithm for sharing. phpurl Conversion Algorithm

Source: Internet
Author: User

PHP converts a URL to a short URL Algorithm for sharing. phpurl Conversion Algorithm

Preface

The short URL service may be no longer unfamiliar to many of my friends. Now, there are many application modes in most Weibo, mobile phone email reminders and other places, and they occupy a certain market. It is estimated that many of our friends are using the service now.

Benefits of short links:

1. content needs;
2. User-friendly;
3. easy to manage.

The following is an algorithm for short URL conversion using PHP. The Code is as follows:

PHP

<? Php // short url generation algorithm class upload url {// empty table public static $ charset = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; public static function encode ($ url) {$ key = 'abc '; // Add salt $ urlhash = md5 ($ key. $ url); $ len = strlen ($ urlhash); // divide the encrypted string into four segments, each of which is 4 bytes, A total of four short connections can be generated for ($ I = 0; $ I <4; $ I ++) {$ urlhash_piece = substr ($ urlhash, $ I * $ len/4, $ len/4); // bitwise AND 0x3fffffff are bitwise AND, 0x3fffffffff indicates binary 30 digits of 1, that is, the encrypted strings after 30 digits are all set to zero // hexdec () is used to convert the hexadecimal string to a 10-digit numeric type, otherwise the operation will not work properly $ hex = hexdec ($ urlhash_piece) & 0x3fffffff; // enter $ short_url = "http://t.cn/" as required for the domain name /"; // generate 6 short URLs for ($ j = 0; $ j <6; $ j ++) {// convert the value to 0x0000003d, and the 3d value to 61, that is, the maximum coordinate value of charset $ short_url. = self: $ charset [$ hex & 0x0000003d]; // After the loop is completed, move the hex right to 5 bits. $ hex = $ hex> 5 ;} $ short_url_list [] = $ short_url;} return $ short_url_list;} $ url = "http://www.sunblog Er.com/"; $ short = signed url: encode ($ url); print_r ($ short);?>

We usually use the first group in the four groups of URLs.

Note that this algorithm is irreversible. Therefore, the common practice is to store the short URL and the corresponding original URL to the database, retrieve the matched original URL from the database and jump to the URL through 301 or header.

Summary

The above is all about this article. I hope to help you in your study or work. If you have any questions, please leave a message.

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.