<?php /*Created By RexLee 投票类Vote **PHP file Getip.php 2012-9-12 */ class Vote { private $errmsg;//当前错误信息:1第一次投票成功,2再次投票成功,-1投票项错误,-2请明天再投 private $time;//当前日期时间 private $ip;//IP private $link;//连接句柄 private $db;//数据库句柄 private $host='localhost';//主机名 private $name='root';//数据库用户名 private $password='lijun';//数据库用户密码 private $dbname='toupiao';//数据库名 private $t=30;//投票时间间隔【秒】 private $MAXvotes=5;//最大项目数 public function __toString(){ //1第一次投票成功,2再次投票成功,-1投票项错误,-2请明天再投 switch ($this->errmsg) { case 1: return "第一次投票成功"; break; case 2: return "再次投票成功"; break; case -1: return "投票项错误"; break; case -2: return "请明天再投"; break; default: ; break; } } private function conn() {//建立数据库连接 $this->link=mysql_connect($this->host,$this->name,$this->password) or die("主机连接失败!");//TODO Connect Database $charset='utf8'; mysql_query("set names ".$charset);//设置字符集 $this->db=mysql_select_db($this->dbname,$this->link);//TODO Open Database } public function __construct($item) { $this->time=time(); $this->ip=$this->getIP();//当前IP $this->conn(); if ($item>$this->MAXvotes||$item<0||!is_int($item)) { //echo '投票项错误'; $this->errmsg=-1;//投票项错误 } if($this->chkVoteAble($this->getIP())){ //echo '可以投票'; $this->vot($item); }else { //echo '当前IP不可投票'; $this->errmsg=-2;//当前IP不可投票,请明天再投票 } } private function chkVoteAble($ip) {//TODO检验是否能够投票 $sql="select ip,date from user where ip='$ip'"; $result=mysql_query($sql); if (!$result) { return true;//数据库中没有记录,可以投票 }else { $row=mysql_fetch_row($result); if(time()>$row[1]/*date*/+$this->t){//超出限制期,可投 return true; }else { return false; }; } } private function vot($item) {//TODO投票 if (is_int($item)) { //写用户数据库 $sql_chkuser="select ip from user where ip='{$this->ip}'"; if(mysql_fetch_array(mysql_query($sql_chkuser))){ $this->errmsg=2;//再次投票成功; mysql_query("UPDATE user set date={$this->time} where ip='{$this->ip}'");//更新当前时间 }else{ //数据库中没有当前IP的记录 mysql_query("insert user VALUES('','{$this->ip}',{$this->time})"); $this->errmsg=1;//第一次投票成功 }; //写投票数据库 $sql="select id from item where id={$item}"; $result=mysql_query($sql); if (!mysql_fetch_row($result)) { $this->errmsg=-1;//echo "没有该项,错误"; return false; }else{ $sql_getvotes="select votes from item where id={$item}"; $votes=mysql_fetch_row(mysql_query($sql_getvotes)); $votes=$votes[0]; $votes++; $sql_setvotes="UPDATE item set votes={$votes} where id={$item}";//TODO 票数加一 if(mysql_query($sql_setvotes)){ return true; }else { return false;//SQL语法错误,失败,不可能发生 } } }else { return false;//参数错误 }; } private function getIP(){//获取用户IP if(getenv('HTTP_CLIENT_IP')) { $onlineip = getenv('HTTP_CLIENT_IP'); } elseif(getenv('HTTP_X_FORWARDED_FOR')) { $onlineip = getenv('HTTP_X_FORWARDED_FOR'); } elseif(getenv('REMOTE_ADDR')) { $onlineip = getenv('REMOTE_ADDR'); } else { $onlineip = $HTTP_SERVER_VARS['REMOTE_ADDR']; } return $onlineip; } } ?>
2. vo.php
<?php /*Created By RexLee **PHP file test.php 2012-9-12 */ header("Content-type:text/html;charset=utf-8"); include 'Vote.php'; $ip=new Vote((int)$_GET['i']); echo $ip; ?>
3.toupiao.sql
-- phpMyAdmin SQL Dump -- version 3.5.2.2 -- http://www.phpmyadmin.net -- -- 主机: localhost -- 生成日期: 2012 年 09 月 12 日 17:53 -- 服务器版本: 5.5.24-log -- PHP 版本: 5.4.3 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- 数据库: `toupiao` -- -- -------------------------------------------------------- -- -- 表的结构 `item` -- CREATE TABLE IF NOT EXISTS `item` ( `Id` int(11) NOT NULL AUTO_INCREMENT COMMENT '条目', `votes` int(11) NOT NULL DEFAULT '0' COMMENT '票数', PRIMARY KEY (`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='条目' AUTO_INCREMENT=36 ; -- -- 转存表中的数据 `item` -- INSERT INTO `item` (`Id`, `votes`) VALUES (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0), (8, 0), (9, 0), (10, 0), (11, 0), (12, 0), (13, 0), (14, 0), (15, 0), (16, 0), (17, 0), (18, 0), (19, 0), (20, 0), (21, 0), (22, 0), (23, 0), (24, 0), (25, 0), (26, 0), (27, 0), (28, 0), (29, 0), (30, 0), (31, 0), (32, 0), (33, 0), (34, 0), (35, 0); -- -------------------------------------------------------- -- -- 表的结构 `user` -- CREATE TABLE IF NOT EXISTS `user` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `ip` varchar(15) NOT NULL DEFAULT '' COMMENT '用户IP', `date` int(11) NOT NULL DEFAULT '0' COMMENT '由int型日期判断是否可以投票', PRIMARY KEY (`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ; -- -- 转存表中的数据 `user` -- INSERT INTO `user` (`Id`, `ip`, `date`) VALUES (1, '127.0.0.11', 0), (2, '192.168.1.100', 0), (3, '192.168.1.101', 0), (4, '127.0.0.12', 1347469612), (5, '127.0.0.1', 1347471829); /*!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 */;