aliyaghobi
کاربر عضو
سلام دوستان
یه کوتاه کننده لینک دارم چطوری میشه ip کاربر رو در دیتابیس ذخیره کنم؟!
با سپاس
یه کوتاه کننده لینک دارم چطوری میشه ip کاربر رو در دیتابیس ذخیره کنم؟!
کد:
<?php
/**
* UrlShortener Class
*
*
* Database Create :
* CREATE TABLE `url_shortener`.`urls` (
* `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
* `url` VARCHAR( 500 ) NOT NULL ,
* `short_code` VARCHAR( 15 ) NOT NULL ,
* `visits` int NOT NULL ,
* `create_time` TIMESTAMP NOT NULL
* ) ENGINE = MYISAM ;
*/
class UrlShortener
{
private $pdo;
function __construct()
{
$this->pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME,DB_USERNAME,DB_PASSWORD);
}
/**
* Create short code
*
*/
function createShortCode()
{
$chars = "1234567890abcdefghijklmnopqrstuvwxyz";
$short_code = '';
while(strlen($short_code) < 4)
{
$short_code .= $chars[rand(0,strlen($chars))];
}
// check in db
$stm = $this->pdo->prepare('select * from urls where short_code = :short');
$stm->execute(array('short'=>$short_code));
$res = $stm->fetch();
print_r($res);
return $short_code;
}
/**
* return true if url format valid
*
*/
function validUrl($url)
{
return filter_var($url , FILTER_VALIDATE_URL , FILTER_FLAG_HOST_REQUIRED);
}
/**
* Check url exist in db
* @param $url String
*/
function existInDb($url)
{
$stm = $this->pdo->prepare('select * from urls where url = \''.$url.'\'');
$stm->execute();
$res = $stm->fetch();
return ( empty($res['short_code']) ? false : $res['short_code']);
}
function insertInDb($url)
{
// if url exist in db return short code
if(($short_code = $this->existInDb($url)) !== false)
{
return $short_code;
}
// insert in db and return short code
if($this->validUrl($url))
{
$short_code = $this->createShortCode($url);
$stm = $this->pdo->prepare('insert into urls (url , short_code,create_time)values(:url,:short_code,:time)');
$param = array('url'=>$url,'short_code'=>$short_code,'time'=>date('Y-m-d-H:i:s'));
$stm->execute($param);
return $short_code;
}else
{
return 'invalid';
}
return false;
}
/**
* return url
*/
function getUrl($short_code)
{
$stm = $this->pdo->prepare('select * from urls where short_code = :short');
$stm->execute(array('short'=>$short_code));
$result = $stm->fetch();
return $result['url'];
}
function addCount($url)
{
$stm = $this->pdo->prepare('update urls set visits = visits +1 where url = :url');
$stm->execute(array('url'=>$url));
}
}