[源碼]簡易投票系統

文件架構:

bollat
..include
....check.func.php
....common.inc.php
....count.inc.php
....global.func.php
....input.inc.php
....mysql.func.php
..style
....basic.css
..index.php
..count.php

check.func.php

<?php
/**
* Version1.0
* ================================================
* Copy 2015-2020 qingfeng
* Email:[email protected]
* ================================================
* Author: qingfeng
* Date: 2015年8月4日
*/
	if(!defined("IN_BOLLAT")){
		exit('Access Not Defined!');
	}
	function _check_uniqid($_first_uniqid,$_end_uniqid) {
    
        if ((strlen($_first_uniqid) != 40) || ($_first_uniqid != $_end_uniqid)) {
            _alert_back('唯一標識符異常');
        }
        return _mysql_string($_first_uniqid);
    }
    function _check_username($_string) {
        //去掉兩邊的空格
        $_string = trim($_string);
        //長度限制
        if (mb_strlen($_string,'utf-8') <2 || mb_strlen($_string,'utf-8') >3) {
            _alert_back('用戶名格式錯誤!');
        }
        return _mysql_string($_string);
	}
	function _check_tel($_string) {
        if (!preg_match('/^1[\d]{10}$/',$_string)) {
            _alert_back('手機號碼格式不正確!');
        }
        return _mysql_string($_string);
    }
    function _check_qq($_string) {
        if (empty($_string)) {
            return null;
        } else {
            //123456
            if (!preg_match('/^[1-9]{1}[\d]{4,9}$/',$_string)) {
                _alert_back('QQ號碼不正確!');
            }
        }
        return _mysql_string($_string);
    }
?>

common.inc.php

<?php
/**
* Version1.0
* ================================================
* Copy 2015-2020 qingfeng
* Email:[email protected]
* ================================================
* Author: qingfeng
* Date: 2015年8月4日
*/
	if(!defined("IN_BOLLAT")){
		exit('Access Not Defined!');
	}
	header('Content-Type:text/html;charset=utf-8');
	define('ROOT_PATH',substr(dirname(__FILE__),0,-8));
	date_default_timezone_set('Asia/Shanghai');
	require ROOT_PATH.'includes/mysql.func.php';
	require ROOT_PATH.'includes/global.func.php';
	require ROOT_PATH.'includes/check.func.php';
	define('DB_HOST','localhost');
	define('DB_NAME','*******');
	define('DB_USER','root');
	define('DB_PWD','********');
	_connect();
	_select_db();
	_set_names();
?>

count.inc.php

<?php
/**
* Version1.0
* ================================================
* Copy 2015-2020 qingfeng
* Email:[email protected]
* ================================================
* Author: qingfeng
* Date: 2015年7月31日
*/
    //防止惡意調用
    if (!defined('IN_BOLLAT')) {
        exit('Access Not Defined!');
    }
?>
<div id="count">
	<p>關於高三一班同學聚會投票的統計</p>
	<dl>
		<dd>贊成:<?php echo $_clean['agree'];?>票</dd>
		<dd>反對:<?php echo $_clean['against'];?>票</dd>
		<dd>棄投:<?php echo $_clean['waiver'];?>票</dd>
		<dd>共投:<?php echo $_clean['count'];?>票</dd>
	</dl>
		投票人員資料:
		<table>
			<tr><th>姓名</th><th>手機號碼</th><th>QQ號碼</th></tr>
		<?php
			$_html = array();
	    	while (!!$_rows = _fetch_array_list($_count)) {
	    		$_html['name']=$_rows['name'];
				$_html['tel']=$_rows['tel'];
				$_html['qq']=$_rows['qq'];
				echo '<tr><td>'.$_html['name'].'</td><td>'.$_html['tel'].'</td><td><a href="http://wpa.qq.com/msgrd?v=3&uin='.$_html['qq'].'&site=qq&menu=yes" target="_blank">'.$_html['qq'].'</a></td></tr>';
			}
		?>
		</table>
</div>

global.func.php

<?php
/**
* Version1.0
* ================================================
* Copy 2015-2020 qingfeng
* Email:[email protected]
* ================================================
* Author: qingfeng
* Date: 2015年7月1日
*/
    //防止惡意調用
    if (!defined('IN_BOLLAT')) {
        exit('Access Not Defined!');
    }
	/**
	 * 
	 * @param  $_info
	 * @param  $_url
	 */
	function _location($_info,$_url) {
		if (!empty($_info)) {
			echo "<script type='text/javascript'>alert('$_info');location.href='$_url';</script>";
			exit();
		} else {
			header('Location:'.$_url);
		}
	}
	function _alert_back($_info) {
	    echo "<script type='text/javascript'>alert('$_info');history.back();</script>";
	    exit();
	}
	function _sha1_uniqid() {
	    return _mysql_string(sha1(uniqid(rand(),true)));
	}
	function _setcookies($_username,$_uniqid,$_time) {
        //瀏覽器進程
        setcookie('tel',$_username);
        setcookie('uniqid',$_uniqid);
	}
	function _mysql_string($_string) {
	    //get_magic_quotes_gpc()如果開啓狀態,那麼就不需要轉義
	    if (!GPC) {
	        if (is_array($_string)) {
	            foreach ($_string as $_key => $_value) {
	                $_string[$_key] = _mysql_string($_value);   //這裏採用了遞歸,如果不理解,那麼還是用htmlspecialchars
	            }
	        } else {
	            $_string = mysql_real_escape_string($_string);
	        }
	    }
	    return $_string;
	}
	function _session_destroy() {
	    if (session_start()) {
	        session_destroy();
	    }
	}
	function _get_ip(){
	    $ip=false;
	    if(!empty($_SERVER["HTTP_CLIENT_IP"])){
	        $ip = $_SERVER["HTTP_CLIENT_IP"];
	    }
	    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
	        $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
	        if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
	        for ($i = 0; $i < count($ips); $i++) {
	            if (!eregi ("^(10|172.16|192.168).", $ips[$i])) {
	                $ip = $ips[$i];
	                break;
	            }
	        }
	    }
	    return _mysql_string($ip ? $ip : $_SERVER['REMOTE_ADDR']);
	}
?>

input.inc.php

<?php
/**
* Version1.0
* ================================================
* Copy 2015-2020 qingfeng
* Email:[email protected]
* ================================================
* Author: qingfeng
* Date: 2015年7月31日
*/
    //防止惡意調用
    if (!defined('IN_BOLLAT')) {
        exit('Access Not Defined!');
    }
?>
<div id='count'>
	<form method="post" action="?action=input">
		<dl>
			<input type="hidden" name="uniqid" value="<?php echo $_uniqid;?>"  />
			<dd>請填寫您的姓名:<input type="name" name="name"/>*必填</dd>
			<dd>請填寫您的手機:<input type="tel" name="tel" maxlength="11"/>*必填</dd>
			<dd class="submit"><input type="submit" name="submit" value="提交"/></dd>	
		</dl>
	</form>
</div>

mysql.func.php

<?php
/**
* Version1.0
* ================================================
* Copy 2015-2020 qingfeng
* Email:[email protected]
* ================================================
* Author: qingfeng
* Date: 2015年7月1日
*/
    //防止惡意調用
    if (!defined('IN_BOLLAT')) {
        exit('Access Not Defined!');
    }
	/**
	 * 連接數據庫
	 */
	function _connect(){
		global $_conn;
		if(!$_conn=@mysql_connect(DB_HOST,DB_USER,DB_PWD)){
			exit('數據庫連接失敗');
		}
	}
	/**
	 * 選中數據庫
	 */
	function _select_db() {
		if (!mysql_select_db(DB_NAME)) {
			exit('找不到指定的數據庫');
		}
	}
	/**
	 * 設置字符集
	 */
	function _set_names() {
		if (!mysql_query('SET NAMES UTF8')) {
			exit('字符集錯誤');
		}
	}
	function _query($_sql) {
	    if (!$_result = mysql_query($_sql)) {
	        exit('SQL執行失敗'.mysql_error());
	    }
	    return $_result;
	}
	function _fetch_array($_sql) {
	    return mysql_fetch_array(_query($_sql),MYSQL_ASSOC);
	}
	function _affected_rows() {
	    return mysql_affected_rows();
	}
	function _free_result($_result) {
	    mysql_free_result($_result);
	}
	function _num_rows($_result) {
	    return mysql_num_rows($_result);
	}
	/**
	 * _fetch_array_list可以返回指定數據集的所有數據
	 * @param $_result
	 */
	
	function _fetch_array_list($_result) {
	    return mysql_fetch_array($_result,MYSQL_ASSOC);
	}
	/**
	 * 
	 * @param  $_sql
	 * @param  $_info
	 */
	function _is_repeat($_sql) {
	    if (_fetch_array($_sql)) {
	        return true;
	    }
	    return false;
	}
	/**
	 * 關閉數據庫連接
	 */
	function _close() {
		if (!mysql_close()) {
			exit('關閉異常');
		}
	}
?>

basic.css

@CHARSET "UTF-8";
*{
	margin:0;
	padding:0;
	background-color:#ccc;
}
#body{
	margin:0;
	padding:0;
	font-size:30px;
}

p{
	font-size:40px;
	text-align: center;
	margin:10px;
}

#main{
	text-align:center;
	margin:5% 0 0 0;
}
#main form{
	margin:20px 0 0 0;
}
#main form dl{
	margin:0 0 20px 0;
}
#main form dl dd{
	margin:5px 0 0 0;
}
#main form dl dd.submit{
	margin:20px 0 0 0;
}



#count{
	margin:10% 0 0 0;	
	text-align:center;
}
#count table {
	text-align:center;
	margin:0 35% 0 35%;
}
#count table tr th{
	width:150px;
	text-align:center;
}

count.php

<?php
/**
* Version1.0
* ================================================
* Copy 2015-2020 qingfeng
* Email:[email protected]
* ================================================
* Author: qingfeng
* Date: 2015年7月31日
*/
	session_start();
	define('IN_BOLLAT','true');
	require 'includes/common.inc.php';
	if(isset($_COOKIE['tel'])&&isset($_COOKIE['uniqid'])){
		$_agree=_query("SELECT ID FROM syzx WHERE view=1");
		$_against=_query("SELECT ID FROM syzx WHERE view=0");
		$_waiver=_query("SELECT ID FROM syzx WHERE view=2");
		$_count=_query("SELECT ID,name,tel,qq FROM syzx");
		$_clean=array();
		$_clean['agree']=_num_rows($_agree);
		$_clean['against']=_num_rows($_against);
		$_clean['waiver']=_num_rows($_waiver);
		$_clean['count']=_num_rows($_count);
		_session_destroy();
	}else{
		if($_GET['action']=='input'){
			$_clean=array();
			$_clean['uniqid']=_check_uniqid($_POST['uniqid'],$_SESSION['uniqid']);
			$_clean['name']=_check_username($_POST['name']);
			$_clean['tel']=_check_tel($_POST['tel']);
			if(!!$_rows=_fetch_array("SELECT 
											tel,uniqid 
									   FROM 
											syzx 
									  WHERE 
											name='{$_clean['name']}' 
									    AND 
											tel='{$_clean['tel']}'
										")
			){
				_session_destroy();
				_setcookies($_rows['tel'], $_rows['uniqid'],'0');
				_location(NULL,'count.php');
			}else{
				_session_destroy();
				_location('您沒有投票!','index.php');
			}
		}else{
			$_SESSION['uniqid']=$_uniqid=_sha1_uniqid();
		}
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>2011屆嵩縣實實驗中學高三一班</title>
<link rel="stylesheet" type="text/css" href="style/basic.css"/>
</head>
<body>
	<?php 
		if(isset($_COOKIE['tel']) && isset($_COOKIE['uniqid'])){
			require ROOT_PATH.'includes/count.inc.php';
		}else{
			require ROOT_PATH.'includes/input.inc.php';
		}
	?>
</body>
</html>

index.php

<?php
/**
* Version1.0
* ================================================
* Copy 2015-2020 qingfeng
* Email:[email protected]
* ================================================
* Author: qingfeng
* Date: 2015年8月4日
*/
	session_start();
	define('IN_BOLLAT',TRUE);
	require 'includes/common.inc.php';
	if($_GET['action']=='ballot'){
		$_clean=array();
		$_clean['uniqid']=_check_uniqid($_POST['uniqid'], $_SESSION['uniqid']);
		$_clean['name']=_check_username($_POST['name']);
		$_clean['tel']=_check_tel($_POST['tel']);
		$_clean['qq']=_check_qq($_POST['qq']);
		$_clean['ballot']=$_POST['ballot'];
		$_clean['ip']=_get_ip();
		if(!!$_rows=_fetch_array("SELECT 
									tel,uniqid
							   FROM 
									syzx 
							  WHERE 
									name='{$_clean['name']}' 
							     OR 
									tel='{$_clean['tel']}'
							     OR	
									ip='{$_clean['ip']}'
								")
			){
				_setcookies($_rows['tel'], $_rows['uniqid'],'0');
				_location('您已投過票!','count.php');
			}else{
				_query("INSERT INTO 
								syzx (
										uniqid,
										name,
										tel,
										qq,
										view,
										time,
										ip
									 )
						 VALUES(
						 				'{$_clean['uniqid']}',
									    '{$_clean['name']}',
									    '{$_clean['tel']}',
									    '{$_clean['qq']}',
									    '{$_clean['ballot']}',
									    NOW(),
									    '{$_clean['ip']}'
					)");
			if(_affected_rows() == 1){
	    		_close();
				_session_destroy();
				_setcookies($_clean['tel'], $_clean['uniqid'],'0');
	    		_location('投票成功!','count.php');
		    }else{
		        _close();
				_session_destroy();
		        _location('投票失敗!','index.php');
		    }
		}
		exit();
	}else{
		$_SESSION['uniqid']=$_uniqid=_sha1_uniqid();
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>2011屆嵩縣實實驗中學高三一班</title>
<link rel="stylesheet" type="text/css" href="style/basic.css"/>
</head>
<body>
	<div id="main">
		<p>關於2015年春節同學聚會的投票</p>
		<form method="post" action="?action=ballot">
			<dl>
				<input type="hidden" name="uniqid" value="<?php echo $_uniqid; ?>" />
				<dd>
					<label>贊成:<input type="radio" name="ballot" value="1" checked="checked"/>  </label>
					<label> 反對:<input type="radio" name="ballot" value="0"/> </label>
					<label>  棄投:<input type="radio" name="ballot" value="2"/></label>
				</dd>
				<dd>請填寫您的姓名:<input type="name" name="name"/>*必填</dd>
				<dd>請填寫您的手機:<input type="tel" name="tel" maxlength="11"/>*必填</dd>
				<dd>請填寫您的QQ:<input type="qq" name="qq" maxlength="10"/> 選填</dd>
				<dd class="submit"><input type="submit" name="submit" value="提交"/></dd>	
			</dl>
		</form>
		<dl>說明:爲了防止惡意投票,同時也爲了方便聯繫,</br>請您登記姓名和手機號碼。</dl>
	</div>
</body>
</html>

數據庫結構:

wKiom1Xe4UjBll-rAAIn2PGLEFE181.jpg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章