بدست آوردن ip کاربر و ذخیره آن در دیتابیس

aliyaghobi

کاربر عضو
سلام دوستان

یه کوتاه کننده لینک دارم چطوری میشه 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));
    }

}
با سپاس

 

aliyaghobi

کاربر عضو
کد مربوط به 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 پیشفرض تو دیتابیس ثبت میشه

با سپاس

 
بالا