PDA

View Full Version : سوال: بدست آوردن ip کاربر و ذخیره آن در دیتابیس



aliyaghobi
شنبه 10 فروردین 1392, 22:25 عصر
سلام دوستان
سایت کوتاه کننده لینک دارم. چطوری میشه 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_US ERNAME,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,:ti me)');
$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));
}

}

SilverLearn
شنبه 10 فروردین 1392, 22:32 عصر
خوب با کد زیر می تونی آی پی بازدید کننده از صفحه رو بریزی تو متغیر


$IPadr = $_SERVER['REMOTE_ADDR'];

بعدش هر کاری خواستی بکنی ...منجمله ریختن در دیتابیس :)

aliyaghobi
شنبه 10 فروردین 1392, 22:37 عصر
باید کد رو اینجا اضافه کنم؟!

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,:ti me)'); $param = array('url'=>$url,'short_code'=>$short_code,'time' =>date('Y-m-d-H:i:s')); $stm->execute($param); return $short_code; }
میشه لطف کنید برام ویرایشش کنید
با سپاس

SilverLearn
شنبه 10 فروردین 1392, 22:44 عصر
البته لازم نیست کد بالا حتما در بین این کد ها باشه....

نکته قابل توجه توی کد ادیت شده اینه که باید در جدول urls شما یک فیلد به نام 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_US ERNAME,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']);
}
$IPadr = $_SERVER['REMOTE_ADDR'];
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,ip)values(:url,:short_code, :ti me,$IPadr)');
$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));
}

}

aliyaghobi
شنبه 10 فروردین 1392, 23:41 عصر
کد مربوط به ip رو به این صورت نوشتم

$ip = $_SERVER['REMOTE_ADDR'];
$stm = $this->pdo->prepare('insert into urls (url , short_code,create_time,ip)values(:url,:short_code, :time,:ip)');
$param = array('url'=>$url,'short_code'=>$short_code,'time'=>date('Y-m-d-H:i:s'),'ip'=>$ip);

اینم جدول دیتابیسم


CREATE TABLE IF NOT EXISTS `urls` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url` varchar(500) NOT NULL,
`short_code` varchar(15) NOT NULL,
`create_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`visits` int(11) NOT NULL,
`ip` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=60 ;

--
-- Dumping data for table `urls`
--

INSERT INTO `urls` (`id`, `url`, `short_code`, `create_time`, `visits`, `ip`) VALUES
(59, 'http://forum.persianscript.ir/f115/%D8%A2%D9%85%D9%88%D8%B2%D8%B4-%D8%B3%D8%A7%D8%AE%D8%AA-%DB%8C%DA%A9-%D9%81%D8%B1%D9%88%D8%B4%DA%AF%D8%A7%D9%87-%D8%A2%D9%86%D9%84%D8%A7%DB%8C%D9%86-%D8%A8%D8%A7-php-9598/', '4mj5', '2013-03-28 17:19:04', 1, '2.179.220.98' ),
(37, 'http://forum.persianscript.ir/f17/%D8%AF%D8%B1%D8%AC-%D8%B2%D9%85%D8%A7%D9%86-%D8%AF%D8%B1-%D8%A7%D8%B3%DA%A9%D8%B1%DB%8C%D9%BE%D8%AA-9561/#post48071', 'y61q', '2013-03-21 20:52:04', 2, '2.179.220.98' );

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

ip رو پیشفرض شو چی قرار بدم تا درست نمایش بده؟! ip خودمو گذاشتم ولی وقتی با ip کس دیگه هم لینک کوتاه میکنم ip پیشفرض نمایش میده
با سپاس

SilverLearn
یک شنبه 11 فروردین 1392, 17:19 عصر
دوست عزیز شما نیازی نیست که مقدار پیش فرضی برای این متغییر انتخاب کنید ...

خوب همونطور که می دونید php یک زبانی هست که از بالا به پایین اجرا میشه ...پس حتما شما کد آی پی بازدید کننده رو اول نوشتی و بعد در خط های بعدی مقدار پیش فرض رو انتصاب دادی ....بنابراین معلومه که همیشه مقدار پیش فرض در متغییر قرار داده میشه