» » Use PHP to generate short URLs

 

Use PHP to generate short URLs

Author: bamboo06 on 9-04-2017, 00:40, views: 5241

627
The normal site to bring the kind of parameters may be very long, especially in the printing of paper products such as corporate brochures to be printed on a long url, then very ugly, and few people will go to remember this site, Although it is now possible to use a two-dimensional code to open a long URL. But people can use short URLs to achieve beautiful links, especially those with limited number of applications such as microblogging.
Use PHP to generate short URLs

Short URL implementation principle is that there is a data table configuration file will be short URL and the actual URL corresponding, when a short URL request, the program jump to the corresponding actual URL up, in order to achieve the URL of the visit.

Method 1: PHP + MySQl implementation of short URL generation and reading
Conventional program we will generate a good short URL and the original site corresponding to a data table, and then for reading use. Let's start by looking at how to generate a unique short URL.
//generate short urls
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 shorturl($url){ 
    $url=crc32($url); 
    $result=sprintf("%u",$url); 
    return code62($result); 
} 
echo shorturl('http://www.goocode.net/'); 
//1EeIv2 

Use 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 database code here I do not write, this is the PHP foundation.
Then we have a link.php used to receive the read url and implement a real jump.
include_once('connect.php'); //connet db
$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.'; 
} 

In code, if the short URL corresponding to the real url, will use the header to jump to the real page, or return 404 code. So we can use such as: http: //yourdomain/link.php? Url = xxx to achieve short URL access.
Continue, we use the URL rewrite rewrite function to achieve such as through the address: http: // yourdomain / xxx to visit.
The following is the rewrite rule:
#Apache rules:
RewriteRule ^/(.*)$ /link.php?url=$1 [L] 
 
#nginx rules: 
rewrite ^/(.*)$ /link.php?url=$1 last; 


Method 2: PHP + ini implements short URL technology
The benefits of using the database for scenario 1 are easy to use, and a large number of short URL queries need to be optimized. And program 2 to give up the database, use the ini configuration, we will be short URL and real URL configuration in the ini file, PHP directly through parse_ini_file () to read the ini file, a few lines of code can achieve short URL jump.
The links.ini file is configured like this:
google= https://www.google.com/ 
facebook= https://www.facebook.com/ 
youtube= https://www.youtube.com/ 
goocode= http://www.goocode.net 

The index.php code can be written like 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 rule.
#Apache rules: 
RewriteRule ^/(.*)$ /index.php?l=$1 [L] 
 
#nginx rules:
rewrite ^/(.*)$ /index.php?l=$1 last; 

In contrast, the second program for small applications, you can also address the array to save the form, you can also do a management interface to maintain these short Web site.

Tags: php, short, url

Category: PHP Scripts / Plugins

Dear visitor, you are browsing our website as Guest.
We strongly recommend you to register and login to view hidden contents.
<
  • 0 Comments
  • 0 Articles
12 November 2017 20:52

Martin

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
PHP is a good source that we use for many purpose but people not know we also generate short urls through PHP language.An buy dissertation define it more clearly about PHP language usage.

<
  • 0 Comments
  • 0 Articles
20 November 2017 20:53

Leonardsax

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
buy cialis vs viagra
<a href="http://cialisdmngj.com/#">cialis cost</a>
can you split cialis pills in half
cialis prices
can buy cialis over counter usa

<
  • 0 Comments
  • 0 Articles
21 November 2017 00:50

AndrewLet

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
buy cialis online with a prescription
<a href="http://cialisdmngj.com/#">cialis cost</a>
best place to buy cialis online
cialis prices
cheap cialis canadian

<
  • 0 Comments
  • 0 Articles
21 November 2017 17:13

BerthaWob

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
loans for people with bad credit
direct payday lenders
<a href="https://loanstrast.com/">365 day loan</a>
loan quote
loans without credit checks
<a href="https://loansfast.us.com/">online payday loans ohio</a>
online payday advance

<
  • 0 Comments
  • 0 Articles
23 November 2017 03:01

Robertven

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
cialis how to buy
<a href="http://cialismbvi.com/#">cialis prices</a>
how to buy cialis in toronto
cialis prices
order cialis no prescription online

<
  • 0 Comments
  • 0 Articles
23 November 2017 16:06

VictorVobby

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
order cialis online pharmacy
<a href="http://cialismbvi.com/#">cialis online</a>
cheap cialis next day delivery
buy cialis online
cialis canada order

how to buy cialis from canada
<a href="http://cialismbvi.com/#">online cialis</a>
genuine cialis sale
online cialis
cialis soft tablets

<
  • 0 Comments
  • 0 Articles
24 November 2017 14:21

SarahMeern

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
small loans without credit checks
online cash register
<a href="https://loanstrast.com/">payday loan direct lenders only</a>
poor credit

<
  • 0 Comments
  • 0 Articles
24 November 2017 14:30

FloydRusty

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
is there fda approved generic viagra
<a href="http://viagramndet.com/#">cost of viagra</a>
sildenafil citrate 100mg kaufen
viagra prices
hoe lang werkt een viagra pill

<
  • 0 Comments
  • 0 Articles
24 November 2017 15:40

LouisTig

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
canadian online viagra
<a href="http://viagramndet.com/#">viagra cost</a>
buy 1000 viagra
cost of viagra
viagra and extenze together

<
  • 0 Comments
  • 0 Articles
25 November 2017 14:16

Mathewscuro

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
viagra tablets online in india
<a href="http://viagramhbfe.com/#">viagra price</a>
bueno sildenafil generico
viagra prices
sildenafil prices

<
  • 0 Comments
  • 0 Articles
25 November 2017 14:49

Geraldrep

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
has anybody ordered viagra online
<a href="http://viagramhbfe.com/#">viagra prices</a>
generic viagra fast shipping
viagra prices
viagra canadian online pharmacy

<
  • 0 Comments
  • 0 Articles
26 November 2017 00:38

Donaldruish

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
where i buy viagra
<a href="http://viagramhbfe.com/#">viagra price</a>
viagra for men online purchase
viagra prices
pictures viagra pills

<
  • 0 Comments
  • 0 Articles
26 November 2017 02:18

Jamesdiext

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
get viagra new zealand
<a href="http://viagramhbfe.com/#">best price for viagra</a>
viagra generic pills
viagra coupon
price increase viagra

<
  • 0 Comments
  • 0 Articles
26 November 2017 13:01

Bettyjeaxy

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
loans las vegas
quick loans
<a href="https://loanstrast.com/">how to get a payday loan</a>
personal loans for poor credit

<
  • 0 Comments
  • 0 Articles
26 November 2017 16:22

JarrodWal

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
cheap viagra cialis uk
<a href="http://cialisopghe.com/#">coupon for cialis</a>
buy viagra cialis levitra online
cialis coupons printable
cheap legal cialis

<
  • 0 Comments
  • 0 Articles
27 November 2017 15:53

PatrickExork

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
buy cialis no prescription mastercard
<a href="http://cialisopghe.com/#">cialis coupon 20 mg</a>
buy cialis japan
coupon for cialis
can cialis pills cut

buy levitra uk online
<a href="http://levitraklnbi.com/#">9 levitra at walmart</a>
levitra buy online no prescription
levitra 20mg
how do i buy levitra

<
  • 0 Comments
  • 0 Articles
27 November 2017 16:34

VictorLen

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
levitra buy in uk
<a href="http://levitraklnbi.com/#">generic levitra</a>
buy generic levitra 10 mg
levitra online
buy levitra cialis viagra

<
  • 0 Comments
  • 0 Articles
28 November 2017 02:30

RalphPiodo

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
order levitra online
<a href="http://levitraklnbi.com/#">viagra vs cialis vs levitra</a>
buy levitra uk
levitra prices
buy levitra and cialis

<
  • 0 Comments
  • 0 Articles
28 November 2017 08:15

YvonneFer

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
online loans
loans online
<a href="https://loanstrast.com/">loans online</a>
payday loans bad credit

<
  • 0 Comments
  • 0 Articles
28 November 2017 13:20

Douglasdiuse

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
buy black cialis
<a href="http://cialisfbvne.com/#">cialis without doctor</a>
order cialis with paypal
cialis without doctor
buy cheap generic cialis

<
  • 0 Comments
  • 0 Articles
29 November 2017 01:07

EricaSeemi

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
payday loans no credit check
payday loans bad credit
<a href="https://loanstrast.com/">payday loans</a>
payday loans online

<
  • 0 Comments
  • 0 Articles
29 November 2017 07:38

Derekfoend

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
how to buy cialis in australia
<a href="http://cialisfbvne.com/#">cialis without a prescription</a>
buy real cialis cheap
cialis without a doctor prescription
where is the best place to buy cialis online

<
  • 0 Comments
  • 0 Articles
29 November 2017 07:56

JamesGuify

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
cialis sale montreal
<a href="http://cialisfbvne.com/#">cialis without a doctor's prescription</a>
real cialis pills
cialis without a doctor prescription
discount on cialis

<
  • 0 Comments
  • 0 Articles
29 November 2017 15:21

DennistearI

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
viagra online risiken
<a href="http://viagramnkjm.com/#">viagra without a doctor</a>
viagra price in pakistan karachi
viagra without doctor
is it easy to get prescribed viagra

<
  • 0 Comments
  • 0 Articles
30 November 2017 00:07

Susannatierb

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
online loans
payday advance
<a href="https://smajloans.com/">cash advance</a>
payday loans
payday loans no credit check
<a href="https://loanstrast.com/">cash loans</a>
online payday loans
pay day loans
<a href="https://loansfast.us.com/">payday loan online</a>
payday loans online

<
  • 0 Comments
  • 0 Articles
30 November 2017 04:32

Brandonnem

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
quantas mg de viagra tomar
<a href="http://viagramnkjm.com/#">viagra without a doctor’s prescription</a>
buy viagra tokyo
viagra without a doctor
buying viagra in indonesia

<
  • 0 Comments
  • 0 Articles
1 December 2017 15:28

GeorgeLip

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
australie online casino
<a href="http://online-casino.party/#">casino online</a>
online craps us players
online casinos
best website for online roulette

<
  • 0 Comments
  • 0 Articles
1 December 2017 17:01

CraigPsype

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
real casino for real money
<a href="http://online-casino.party/#">casino</a>
downloan blackjack play for real cash
casino online
online games to make real money

<
  • 0 Comments
  • 0 Articles
2 December 2017 14:29

Leighwrili

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
holland casino poker online
<a href="http://online-casino.party/#">online casino</a>
safest online gambling sites
online casino
canadian online casinos paypal

<
  • 0 Comments
  • 0 Articles
2 December 2017 14:57

Jasonbeark

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
casino slots online no download
<a href="http://online-casino.party/#">casino online</a>
online casino new
casino online
bingo virtual gratis

Previous Next

Add comments

Name:*
E-Mail:
Comment:
Bold Italic Underline Strike | Align left Center Align right | Insert smilies Insert link URLInsert protected URL Select color | Add Hidden Text Insert Quote Convert selected text from selection to Cyrillic (Russian) alphabet Insert spoiler
Security Code: *