标签:php
文件架构:
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:wq2010feng@126.com * ================================================ * 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:wq2010feng@126.com * ================================================ * 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:wq2010feng@126.com * ================================================ * 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:wq2010feng@126.com * ================================================ * 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:wq2010feng@126.com * ================================================ * 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:wq2010feng@126.com * ================================================ * 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:wq2010feng@126.com * ================================================ * 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:wq2010feng@126.com * ================================================ * 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>
数据库结构:
本文出自 “keefe” 博客,转载请与作者联系!
标签:php
原文地址:http://keefe.blog.51cto.com/9548710/1689013