URL shortening is a technique on the World Wide Web in which a Uniform Resource Locator (URL) may be made substantially shorter in length and still direct to the required page. This is achieved by using an HTTP Redirect on a domain name that is short, which links to the web page that has a long URL.
Now I give one source code based on PHP. This class may
1. produce one random string based on the input asscii
2. encode id to one short string.
3. decode one short string to id
4. add checker for short string, e.g. abcd, ab is checker, cd is real short url.
5. use database to store id and url, e.g. mysql sqlite, etc,
6. use file to store id and url,
7. not need to save the short url, the algorithm auto-reverse the short url to id
Download the source from
https://code.google.com/p/shortening-url/
Now suppose you have some place to store id and url, e.g in table
- id, url
Where id is an sequence and the url is indexed. This way each url is unique.
Now you may use
shortURL = f(id);
id = f(shortURL)
Finally, you just change the .htaccess file on your server. That is all. Now you may create one short url service for you or your client freely.
#####################Example##########################:
$string = rand(0, 10000);
echo "ID: " . $string . PHP_EOL;
$test = new XCube_BijectionCode();
$string = $test->BijectionEncode($string);
echo "Short String: " . $string . PHP_EOL;
$string = $test->BijectionDecode($string);
echo "Reverse Short String to ID: " . $string . PHP_EOL;
######################################################
###############URL Shortening Class######################
/* ------------------------------------------------------------------------------
* * File: XCube_BijectionCode.class.php
* * Class: Short URL encode and decode
* * Description: This class may create short url based on alphabet or other asscii
* * Version: 1.0
* * Updated: 5-March-2013
* * Author: Zhao Sam
* * Homepage: http://www.researchbib.com
* *------------------------------------------------------------------------------
* * COPYRIGHT (c) 2012 - 2013 BENNETT STONE
* *
* * The source code included in this package is free software; you can
* * redistribute it and/or modify it under the terms of the GNU General Public
* * License as published by the Free Software Foundation. This license can be
* * read at:
* *
* * http://www.opensource.org/licenses/gpl-license.php
* *
* * This program is distributed in the hope that it will be useful, but WITHOUT
* * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* *------------------------------------------------------------------------------ */
class XCube_BijectionCode {
var $mChecker = array(50, 51,);
//abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
var $mAlphabet = 'OetMl3cbTrwzEis7RXH5gKUkNqafdSFIQ0jDWYhuLyP2JGvZ9p6m1AV4oB8nxC';
var $mBase = 0;
var $mLength = 0;
function __construct($mLength = 0) {
$this->mAlphabet = str_split($this->mAlphabet);
$this->mBase = count($this->mAlphabet);
$this->mLength = $mLength;
}
/**
* cryptAlphabet may produce a random Alphabet string.
*
* @param int $loop
* @return string
*/
function cryptAlphabet($loop = 60) {
$mAlphabet = $this->mAlphabet;
for ($i = 0; $i < $loop; $i++) {
$mNewAlpha = array();
do {
$i = rand(0, $this->mBase) % $this->mBase;
if (!in_array($mAlphabet[$i], $mNewAlpha)) {
$mNewAlpha[] = $mAlphabet[$i];
}
if (count($mNewAlpha) == $this->mBase)
break;
} while (TRUE);
$mAlphabet = $mNewAlpha;
}
return implode('', $mAlphabet);
}
/**
* BijectionEncode may produce one short string based on the input id.
* It is not necessarty to store the short string into DB.
* It can reverse it into id when the clients access.
*
* If you want to add checker on short url, please set mChecker=TRUE.
* The checker length is based on the $this->mChecker length.
*
* @param int $id
* @param bool $mChecker
* @return string
*/
function BijectionEncode($id = 0, $mChecker = TRUE) {
$mShortURL = '';
$id = (int) $id; // to interger
if ($id > 0) {
if ($mChecker) {
$mChecker = '';
foreach ($this->mChecker as $value) {
$mChecker .=$this->mAlphabet[$id % $value];
}
} else {
$mChecker = '';
}
while ($id > 0) {
$mShortURL = $this->mAlphabet[$id % $this->mBase] . $mShortURL;
$id = (int) ($id / $this->mBase);
}
$mShortURL = $mChecker . $mShortURL;
}
return $mShortURL;
}
/**
*
* @param string $mShortURL
* @param bool $mChecker
* @return mixed
*/
function BijectionDecode($mShortURL = '', $mChecker = TRUE) {
if ($mChecker) {
$mChekcerLength = count($this->mChecker);
} else {
$mChekcerLength = 0;
}
if (strlen($mShortURL) <= $mChekcerLength) {
return FALSE;
}
$id = 0;
if ($mChekcerLength) {
$mChecker = substr($mShortURL, 0, $mChekcerLength);
} else {
$mChecker = FALSE;
}
//string to integer
foreach (str_split(substr($mShortURL, $mChekcerLength)) as $value) {
$id = $id * $this->mBase + array_search($value, $this->mAlphabet);
}
if ($mChecker) {
$mNewChecker = '';
foreach ($this->mChecker as $value) {
$mNewChecker .=$this->mAlphabet[$id % $value];
}
//wrong URL
if ($mNewChecker !== $mChecker) {
$id = FALSE;
}
}
return $id;
}
}
###################################################################
######################htaccess###################################
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+)[/]*$ index.php?url=$1
No comments:
Post a Comment