php投票系统简单实现源码
数据库的设计:设计三张表,投票结果统计表(count_voting),投票人记录表(ip_votes),用户表(user).
投票结果统计表用于统计最后的投票记录,我给它弄了4个字段:被投票项的名称(SelectName),被投票项标签名(LabelName)(起到分类的作用),票数(CountVotes).
投票人记录表用于登记投票人的ip(IP),地理位置(Location),投票时间(VoteTime),被投票项名称(SelectName),然后我还给它加一个ID.
用户表主要用于给管理员用的,包含用户名(name)和密码(passwd).
生成表的sql脚本如下,代码如下:
-
-
-
-
- DROP TABLE IF EXISTS `count_voting`;
- CREATE TABLE IF NOT EXISTS `count_voting` (
- `SelectName` varchar(40) NOT NULL,
- `LabelName` varchar(40) NOT NULL,
- `CountVotes` bigint(20) unsigned NOT NULL,
- UNIQUE KEY `SelectName` (`SelectName`),
- KEY `CountVotes` (`CountVotes`),
- KEY `CountVotes_2` (`CountVotes`),
- KEY `CountVotes_3` (`CountVotes`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='投票统计表';
-
-
-
-
-
-
-
- DROP TABLE IF EXISTS `ip_votes`;
- CREATE TABLE IF NOT EXISTS `ip_votes` (
- `ID` bigint(20) unsigned NOT NULL auto_increment COMMENT '投票人序号:自增',
- `IP` varchar(15) NOT NULL COMMENT '投票人IP',
- `Location` varchar(40) NOT NULL COMMENT '投票人位置',
- `VoteTime` datetime NOT NULL,
- `SelectName` varchar(40) NOT NULL,
- PRIMARY KEY (`ID`),
- KEY `ID` (`ID`),
- KEY `SelectName` (`SelectName`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
-
-
-
-
- DROP TRIGGER IF EXISTS `vote_count_after_insert_tr`;
- DELIMITER //
- CREATE TRIGGER `vote_count_after_insert_tr` AFTER INSERT ON `ip_votes`
- FOR EACH ROW UPDATE count_voting SET CountVotes = CountVotes + 1 WHERE SelectName = NEW.SelectName
- //
- DELIMITER ;
-
-
-
-
-
-
-
- DROP TABLE IF EXISTS `user`;
- CREATE TABLE IF NOT EXISTS `user` (
- `name` varchar(10) NOT NULL COMMENT '管理员用户名',
- `passwd` char(32) NOT NULL COMMENT '登录密码MD5值'
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';
-
-
-
-
-
- INSERT INTO `user` (`name`, `passwd`) VALUES
- ('ttxi', '700469ca1555900b18c641bf7b0a1fa1'),
- ('jitttanwa', 'adac5659956d68bcbc6f40aa5cd00d5c');
-
-
-
-
-
-
-
-
- ALTER TABLE `ip_votes`
- ADD CONSTRAINT `ip_votes_ibfk_1` FOREIGN KEY (`SelectName`) REFERENCES `count_voting` (`SelectName`) ON DELETE CASCADE ON UPDATE CASCADE;
从脚本中可以看出,我创建了一个触发器,当往ip_votes表中插入数据的时候就给count_voting表中的CountVotes字段加1,还能后出最后一句是设置外部关联字.
框架设计:OperatorDB类用于操作数据库,OperatorVotingDB类用于该系统特定的操作集合,使用PDO操作数据库,我它简单的封装一下,代码如下:
-
-
-
-
- class OperatorDB
- {
-
- private $dbms='mysql';
- private $host='localhost';
- private $dbName='voting';
- private $user='voting';
- private $passwd='voting';
- private $pdo=null;
-
- public function __construct()
- {
-
-
- $this->dsn="$this->dbms:host=$this->host;dbname=$this->dbName";
- try
- {
- $this->conn=new PDO($this->dsn,$this->user,$this->passwd);
- }
- catch(PDOException $e)
- {
- die("<br/>数据库连接失败(creater PDO Error!): ".$e->getMessage()."<br/>");
- }
- }
- public function __destruct()
- {
- $this->pdo = null;
- }
- public function exec($sql)
- {
- }
- public function query($sql)
- {
- }
- }
把连接数据库的信息封装进去方便后续的操作,代码如下:
- <?php
- require_once 'OperatorDB.php';
- class OperatorVotingDB
- {
- private $odb;
-
- public function __construct()
- {
- $this->odb = new OperatorDB();
- }
- public function __destruct()
- {
- $this->odb = null;
- }
-
-
-
-
-
-
- public function clearTables()
- {
- $sqls = array("TRUNCATE ip_votes;","TRUNCATE count_voting;");
- $this->odb->exec($sqls[0]);
- $this->odb->exec($sqls[1]);
- }
-
-
-
-
-
- public function resetCountValues()
- {
- $sql = "UPDATE count_voting SET CountVotes = 0;";
- $this->odb->exec($sql);
- }
-
-
-
-
-
-
-
-
-
- public function vote($ip,$loc,$name)
- {
- $sql = "INSERT INTO ip_votes VALUES (NULL, '$ip', '$loc', NOW(), '$name')";
- $subsql = "SELECT MAX(to_days(VoteTime)) FROM ip_votes WHERE IP='$ip'";
- $stm = $this->odb->query($subsql);
- if (count($row=$stm->fetchAll())==1)
- {
- $now = date("Y-m-d H:i:s");
- $subsql = "SELECT to_days('$now');";
- $stm = $this->odb->query($subsql)->fetch();
- $time = $stm[0];
-
-
- if ($time-$row[0][0]<1)
- {
- echo "投票失败,相同ip需要隔一天才能投票";
- return;
- }
- }
-
- echo "投票成功!";
- $this->odb->exec($sql);
- }
-
-
-
-
-
-
-
-
- public function addSelectName($name, $label, $count=0)
- {
- $sql = "INSERT INTO count_voting VALUES ('$name', '$label', $count);";
- $this->odb->exec($sql);
- }
-
-
-
-
-
-
-
-
-
- public function getVotesSortByCount($n=-1)
- {
- $sql = "SELECT * FROM count_voting ORDER BY CountVotes DESC LIMIT 0 , $n;";
- if (-1 == $n)
- {
- $sql = "SELECT * FROM count_voting ORDER BY CountVotes DESC;";
- }
-
- return $this->odb->query($sql);
- }
-
-
-
-
-
-
- public function getVotesGroupByLabel()
- {
- $sql = "SELECT * FROM count_voting ORDER BY LabelName DESC;";
-
- return $this->odb->query($sql);
- }
- }
- ?>
下面还有需要的函数,代码如下:
- <?php
-
-
-
-
-
- function goToPgae($url)
- {
- echo "<script language='javascript' type='text/javascript'>";
- echo "window.location.href='$url'";
- echo "</script>";
- }
- function jsFunc($fun, $arg=null)
- {
- echo "<script language='javascript' type='text/javascript'>";
- echo $fun."('$arg');";
- echo "</script>";
- }
- function jsFunc3($fun, $arg1=null,$arg2=null,$arg3=null)
- {
- echo "<script language='javascript' type='text/javascript'>";
- echo $fun."('$arg1','$arg2','$arg3');";
- echo "</script>";
-
- }
-
- function isLoginNow()
- {
- if ($_COOKIE["user"]=='')
- {
- return false;
- }
- return true;
- }
-
- function getClientIP()
- {
- if ($_SERVER["HTTP_X_FORWARDED_FOR"])
- {
- if ($_SERVER["HTTP_CLIENT_IP"])
- {
- $proxy = $_SERVER["HTTP_CLIENT_IP"];
- }
- else
- {
- $proxy = $_SERVER["REMOTE_ADDR"];
- }
- $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
- }
- else
- {
- if ($_SERVER["HTTP_CLIENT_IP"])
- {
- $ip = $_SERVER["HTTP_CLIENT_IP"];
- }
- else
- {
- $ip = $_SERVER["REMOTE_ADDR"];
- }
- }
- return $ip;
- }
-
-
- function getIpfrom123cha($ip) {
- $url = 'http://www.123cha.com/ip/?q='.$ip;
- $content = file_get_contents($url);
- $preg = '/(?<=本站主数据:</li><li style="width:450px;">)(.*)(?=</li>)/isU';
- preg_match_all($preg, $content, $mb);
- $str = strip_tags($mb[0][0]);
-
- $address = $str;
- if($address == '') {
- $address = '未明';
- }
- return $address;
- }
-
-
- function getIpfromBaidu($ip) {
- $url = 'http://www.baidu.com/s?wd='.$ip;
- $content = file_get_contents($url);
- $preg = '/(?<=<p class="op_ip_detail">)(.*)(?=</p>)/isU';
- preg_match_all($preg, $content, $mb);
- $str = strip_tags($mb[0][1]);
- $str = str_replace(' ', '', $str);
- $address = substr($str, 7);
- if($address == '') {
- $address = '未明';
- }
- return $address;
- }
- ?>
然后就是后台管理员的操作怎么弄了,主要是添加投票项的功能,操作数据库上面已经实现,后面的基本上是页面怎么设置,关系到js,添加投票项的页面是动态的,代码如下:
- function addVote()
- {
- right.innerHTML="<h2>添加投票项</h2>";
- right.innerHTML+="<label>投票项标签<label>";
- addInput("right","cLabelName","地区名");
- right.innerHTML+="<br><label>投票项名称<label>";
- addInput("right","cSelectName","学校名");
- right.innerHTML+="<br>";
- var args = ''./add.php','cSelectName','cLabelName'';
- var str = '<input type=button value="u6dfb加" onclick="goToPage('+args+');"/>';
- right.innerHTML+=str;
- }
-
-
- function addInput(parent,id,pla)
- {
-
- var input = document.createElement("input");
- input.type = "text";
- input.id = id;
- input.placeholder = pla;
- document.getElementById(parent).appendChild(input);
- }
添加投票项是通过url传递变量到add.php页面的,代码如下:
- <?php
- require_once '../api/func.php';
-
- if (!isLoginNow())
- {
- goToPgae("./index.php");
- }
-
- $name = $_GET["cSelectName"];
- $label = $_GET["cLabelName"];
-
- require_once '../api/OperatorVotingDB.php';
- $ovdb=new OperatorVotingDB();
- $ovdb->addSelectName($name,$label);
- require './header.htm';
- goToPgae("./admin.php?page=add&auto="."$label"."&id=cLabelName&foc=cSelectName&msg=添加成功");
- ?>
下面是两个跳转页面的函数,js的,上面func.php中的跳转页面函数也是通过js实现的,代码如下:
-
- function goToPage(url,arg1,arg2)
- {
- var a = document.getElementById(arg1).value;
- var b = document.getElementById(arg2).value;
- url += '?'+arg1+'='+a;
- url += '&'+arg2+'='+b;
- window.location.href=url;
- }
-
- function goToPage1(url)
- {
- window.location.href=url;
- }
还有修改删除功能没有实现,我应该不会去实现那个了吧,js的话和添加功能差不多.
登录模块的话网上很多,模仿的,就是提交表单,查找数据库,返回结果,成功则设置cookie,后台的每个页面都添加了检测cookie的功能的.