This is optional feature. Just if you have set limitation for requests, then it would be good to clean expired data after each day.
To do this, you should setup cron command.
All you need is to add following line into your cron job shedule.
0 2 * * * /usr/bin/php /path/to/web/root/app/yiic.php clearstat >/dev/null 2>&1
0 2 * * * |
Time and date for your task. In our case cron will be executed each day at 2:00 a.m |
/usr/bin/php |
path to php command |
/path/to/web/root/app/yiic.php |
physical path to script |
clearstat |
Command name |
>/dev/null 2>&1 |
this mean that all output from this command should be shoved into a black hole. I.e just do nothing with output. |
API Documentation
Developers can easy use this API by request http://domain.name/api?url=your_long_url URL.
This service can handle 3 response methods:
XML
JSON
Array (serialized)
Example:
http://domain.name/api?url=url&method=array
You will get response as serialized array
PHP Code
Note. This app based on Yii framework.
All backend and frontend controllers are located in /web/root/dir/app/controllers/{backend/frontend}/
All backend and frontend views are located in /web/root/dir/app/views/{backend/frontend}/
Config file located in /web/root/dir/app/config/main.php
DB Config file located in /web/root/dir/app/config/db.php
Shortener Algorithm
This algorithm based on Bijective Function which allows you to use maximum of possible records to store. For example maximum value of unsigned BIGINT field is 18 446 744 073 709 551 615 the short id will be only v8QrKbgkrAq
How to short
File: /web/root/dir/app/controllers/frontend/ApplicationController.php
Function: ActionShort
// Get the last insert id
$id = Shortlink::model()->insertLink($url);
//Get short link ID by integer
$shortLink = Yii::app()->shortlink->shortByID($id);
//And insert short ID into DB
Shortlink::model()->updateLinkByID($id, $shortLink)) ;
How to decode
File: /web/root/dir/app/controllers/frontend/ApplicationController.php
Function: ActionRedirect
//Get integer from shortID
$id = Yii::app()->shortlink->getID($shortid);
//Get url by integer
$url = Shortlink::model()->findByPk($id);
Update
Version 0.2
- Added countdown timer before redirect. Be default countdown timer is equal 0, it mean that user will redirect instant. If you want to make more money from ads, you can set timer to 5 seconds and add any ads code in /web/root/app/views/frontend/application/redirect.php
For people who have bought the earlier version:
Firstly, run this 3 queries in db
ALTER TABLE `sl_basic_settings` ADD COLUMN `Redirect` SMALLINT UNSIGNED NOT NULL AFTER `JsScript`;
UPDATE `sl_basic_settings` SET Redirect = 0;
INSERT INTO sl_lang_settings (`Key`, `Value`, `end`) VALUES ('Redirect_Timer', 'You will be redirected to %s in %s second(-s)', 'front');
Overwrite this folders (or all project if you want)
app/models/, app/controllers/, app/views
Remove cache files (/web/root/app/runtime/cache)
Version 0.1
- Bug fix (prevent to shorten the same URL multiple times)