Using PHP to generate a short url scheme the normal url with parameters may be very long, especially if we need to print paper products such as enterprise brochure to print a long url, it is very ugly, in addition, few people will remember this website, although you can scan the QR code to open the long website. However, people can use short URLs to achieve beautiful links, especially for applications with word limit, such as Weibo.
View demo download source code
The implementation principle of a short URL is that a data table will configure the file to correspond the short URL to the actual URL. when requesting a short URL, the program will jump to the corresponding actual URL, to access the website.
Solution 1: generate and read short URLs using PHP + MySQl
In the conventional solution, we map the generated short URL and the original URL to a data table for reading and using. Let's first look at how to generate a unique short URL.
// Generate the short URL function code62 ($ x) {$ show = ''; while ($ x> 0) {$ s = $ x % 62; 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 compute url ($ url) {$ url = crc32 ($ url ); $ result = sprintf ("% u", $ url); return code62 ($ result);} echo response url ('http: // www.helloweba.com/'); // 1EeIv2
Use the above PHP code to generate a unique 6-bit short url. then we will write the generated short URL into the MySQL table together with the original URL. I will not write the code for inserting the database here, this is the foundation of PHP.
Next, we have link. php to receive the read url and implement real jump.
Include_once ('connect. php'); // connect to the database $ url =$ _ GET ['URL']; if (isset ($ url )&&! Empty ($ url) {$ SQL = "select url from your url 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. ';}
In the code, if the real url corresponding to the short url is obtained, the header will be used to jump to the real page; otherwise, code 404 will be returned. In this way, we can use http: // yourdomain/link. php? Url = xxx.
To continue, we can use the URL rewrite function to implement access such as through Address: http: // yourdomain/xxx.
The following are rewrite rules:
# Apache rules: RewriteRule ^/(. *) $/link. php? Url = $1 [L] # if nginx is used, write the following rule: rewrite ^/(. *) $/link. php? Url = $1 last;
Solution 2: PHP + ini short URL technology
The advantage of using the database in solution 1 is that it is easy to operate, and a large number of short URL queries need to be optimized. Solution 2 abandons the database and uses the ini configuration. we configure the short and real URLs in the ini file. PHP directly reads the ini file through parse_ini_file, A few lines of code can be used to redirect short URLs.
The links. ini file is configured as follows:
baidu= https://www.baidu.com/qq= http://www.qq.com/hw= http://www.helloweba.com/dm= http://www.helloweba.com/view-blog-362.html
The index. php code can be written as follows:
$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 rule.
# Apache rules: RewriteRule ^/(. *) $/index. php? L = $1 [L] # if nginx is used, write the following rule: rewrite ^/(. *) $/index. php? L = $1 last;
Now, let's visit the website: Ghost.
In comparison, the second solution is suitable for small applications. you can also save the url address in the form of an array, or create a management interface to maintain these short URLs.