PHP generated short URL method summary _php tips

Source: Internet
Author: User
Tags bitwise chr read ini file

The normal URL with the parameters of the kind may be very long, in particular, we are printed on paper products such as corporate brochure to print a long URL is very ugly, and few people will be to remember this site, although now can be used to scan two-dimensional code to open a long web site. But people can use short URLs to achieve beautiful links, especially with word-limiting applications such as microblogs.

The principle of the short URL is that there is a data table will be the configuration file will be a short URL and the actual URL for the corresponding, when the request for a short URL, the program to jump to the corresponding actual URL up, so as to achieve access to the Web site.

Scheme 1:php+mysql to realize the generation and reading of short URLs

The regular program we will generate a good short URL and the original URL to a data table, and then for reading use. Let's take a look at how to generate a unique short URL.

Generate short URL 
function code62 ($x) { 
  $show = '; 
  while ($x >0) { 
    $s = $x%; 
    if ($s >35) { 
      $s =chr ($s +61); 
    } ElseIf ($s >9&& $s <=35) { 
      $s =chr ($s +55); 
    } 
    $show. = $s; 
    $x =floor ($x/62); 
  return $show; 
} 
function Shorturl ($url) { 
  $url =crc32 ($url); 
  $result =sprintf ("%u", $url); 
  Return code62 ($result); 
echo shorturl (' http://www.jb51.net/'); 
1eeiv2 

Using the above PHP code can generate a unique 6-bit short URL, and then we will generate a short URL with the original URL to write to the MySQL table, insert the code of the database here I do not write, this is the basis of PHP.
Next, we have a link.php to receive the read URL and implement a real jump.

Include_once (' connect.php '); Connection database 
$url = $_get[' url ']; 
if (Isset ($url) &&!empty ($url)) { 
  $sql = "Select URL from shorturl where codeid= ' $url '"; 
  $query = mysql_query ($sql); 
  if ($row =mysql_fetch_array ($query)) { 
    $real _url = $row [' url ']; 
    Header (' Location: '. $real _url); 
  else{ 
    header (' http/1.0 404 Not Found '); 
    Echo ' Unknown link. '} 
} else{ 
  header (' http/1.0 404 Not Found '); 
  Echo ' Unknown link. '; 
} 

Code, if you get a short URL corresponding to the real URL, will use header to jump to the real page, otherwise return 404 code. This allows us to use such as: http://yourdomain/link.php?url=xxx to achieve short URL access.

Continue, we use the URL rewrite that overrides the function to implement such as can be accessed via address: http://yourdomain/xxx.

The following are the rewrite rules:

#Apache规则: 
rewriterule ^/(. *) $/link.php?url=$1 [L] 
 
#如果使用nginx, the rule writes: 
rewrite ^/(. *) $/link.php?url= $ last; 

Scheme 2:php+ini implement short URL technology

For Scenario 1, the advantage of using a database is ease of operation, and a large number of short URL queries need to be optimized. and Scheme 2 discard the database, use the INI configuration, we will short URLs and real URL configuration in the INI file, PHP directly through Parse_ini_file () read INI file, a few lines of code can achieve a short URL jump.
Links.ini files are configured like this:

Baidu  = https://www.baidu.com/ 
qq    = http://www.qq.com/ 
HW    = http://www.hw.com/ 
DM    = Http://www.dm.com/view-blog-362.html 

And index.php's code can write this:

$links = Parse_ini_file (' Links.ini '); 
 
if (Isset ($_get[' l ')) && array_key_exists ($_get[' l '], $links)) { 
  header (' Location: '. $links [$_get[' L ']] ); 
} 
else{ 
  header (' http/1.0 404 Not Found '); 
  Echo ' Unknown link. '; 
} 

Of course, we also need to configure the rewrite rules.

#Apache规则: 
rewriterule ^/(. *) $/index.php?l=$1 [L] 
 
#如果使用nginx, the rule writes: 
rewrite ^/(. *) $/index.php?l=$1 Last 

OK, we now visit the URL: HTTP://DEMO.JB51.NET/DM, it directly to the actual URL: http://www.jb51.net/view-blog-362.html.
In contrast, the second scheme is suitable for small applications, you can also save the URL as an array, you can do a management interface dedicated to maintain these short URLs.

The third option: the short link of the PHP version of the delivery method

<?php
function Shorturl ($input) {
$base = array (
' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', '
i ', ' j ', ') ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ',
' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ',
' y ', ' z ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 '
);
 
$hex = MD5 ($input);
$hexLen = strlen ($hex);
$subHexLen = $hexLen/8;
$output = Array ();
 
for ($i = 0; $i < $subHexLen; $i + +) {
//Encrypt characters in 8-bit group 16 and 0X3FFFFFFF (30-bit 1) bitwise AND operation
$subHex = substr ($hex, $i * 8 , 8);
$int = 0X3FFFFFFF & (1 * (' 0x '. $subHex));
$out = ';
 
for ($j = 0; $j < 6; $j + +) {
 
//chars The resulting value to the 0x0000001f bitwise AND operation, obtain the character array index
$val = 0x0000001f & $int;
$out. = $base 32[$val];
$int = $int >> 5;
}
 
$output [] = $out;
}
 
return $output;
>

Summarize:

User access to short URL process:

1, browser access to the short URL http://short.cn/Xvdf23, after DNS resolution will point to the http://short.cn server.
2, the server based on the short URL of the ID field to find a database, return to the original URL.
3, redirect to the original URL returned above

The above is the entire content of this article, I hope you can enjoy

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.