PHP编写的网上调查投票系统

 更新时间:2016年11月25日 16:12  点击:2066

head>
<title>survey</title>
</head>
<body>
<form method="POST" action="survey.php">
  <p><input type="radio" value="0" name="vote">调查项目一</p>
  <p><input type="radio" name="vote" value="1">调查项目二</p>
  <p><input type="radio" name="vote" value="2">调查项目三</p>
  <p><input type="radio" name="vote" value="3">调查项目四</p>
  <p><input type="radio" name="vote" value="4">调查项目五</p>
  <p><input type="hidden" name="go"  value="1">
  <p><input type="submit" value="提交" name="B1"></p>
  <a href="survey.php?result=1">查看结果</a>
</form>
</body>
</html>
 

注意文件data.txt中的调查项目与上面的调查项目在个数和排列顺序必须保持一致,否则会出错或调查的结果不准确。同时为了将调查结果显示成条形图形式,应该准备若干种不同颜色的条形图片。如:0.gif,1.gif,2.gif,3.gif,4.gif等。
以下是实现调查功能的survey.php代码:

以下为引用的内容:
<?
    data="data.txt";
    votes="survey.txt";
    dataf=file(data);        /*读出调查项目文件中的项目*/
     file_votes=fopen(votes, "r");
  line_votes=fgets(file_votes, 255);  /*读出已经记录的调查结果*/
  fclose(file_votes);
  single_vote=explode("|", line_votes); /* 并将数据按指定的字串切开,再将字串传回到数组变量中  */
   if (result!=1)         /*如果已经接受了调查*/
   {
    file_votes=file(votes, "r");
    if (REMOTE_ADDR == file_votes[1])                           /*检查是不是同一个人*/
      {
       echo "<center><font color=red>您已投过票了,谢谢您的参与!</font></center>";
       exit;
      }
    /*如果IP不重复,则执行以下程序*/
    ficdest=fopen(votes, "w");
    for (i=0; i<=count(dataf)-1; i++)
      {
         if (i == vote)
         {                                 /*判断选择了哪个项目*/ Chinaz.com

          single_vote+=1;
         }
             fputs(ficdest, "single_vote|"); /*将数据写回文件*/
      }
       fputs(ficdest, "\nREMOTE_ADDR");/* //写入投票者IP*/
    fclose(ficdest);
    result=1; /*投票成功*/
  }
  /*写入投票结果后并显示投票结果*/
  if (result==1)
  {
   echo "<table cellpadding=10>";
   for (i=0; i<=count(dataf)-1; i++)
    {
     /*取得投票总数*/
     tot_votes+=single_vote;
    }
   for (i=0; i<=count(dataf)-1; i++)
    {
     imag=strval(i).".gif";/*判断用哪种条形图片来显示统计结果*/
      stat=single_vote/tot_votes*100;  /*计算百分比*/
     scla=stat*5;/*条形图和放大倍数,这里是安百分数的5倍的相素的宽度来显示的*/
     echo "<tr><td><li><font face=Verdana size=2>";
     echo "dataf</font></td><td align=left><font face=Verdana size=2>";
     echo "<img src=\"imag\" height=20 width=scla align=middle> ";/*输出条形码图*/
     printf("%.1f", "stat");
     echo "%</font></td><td align=center><font face=Verdana size=2>";
     /*输出本栏目投票数*/
     echo "single_vote</font>";
     echo "</td></tr>";
   }
   echo "</table><p>";
   echo "<font face=Verdana size=2>总投票数:tot_votes </font>";
}
?> 

说明:
在这里为了防止一人多投是采用记录最近的一位投票者的IP的方法来实现的,而最近的一位投票的IP地址是WEB客户机在对服务器发出请求时存储在环境变量REMOTE_ADDR中的。


刚用PHP写了一个网站在线人数的程序,请大家进来指点下!
我是用PHP+MYSQL来写的,原理:网站在线人数的程序代码+后台有MYSQL数据库支持,可以直接统计出网站当前的在线人数。
首先我创建MYSQL数据库表。
CREATE TABLE tablename (
field type(max_length) DEFAULT 'default_value' (NOT) NULL
}可以使用的SQL语句。
CREATE TABLE useronline (
timestamp int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,
file varchar(100) NOT NULL,
PRIMARY KEY (timestamp),
KEY ip (ip),
KEY file (file)
);下面我们是PHP脚本,首先我定义MYSQL的信息。
$server = "localhost"; //你的服务器
$db_user = "root"; //你的mysql的用户名
$db_pass = "password"; //你的mysql的密码
$database = "users"; //表的名字设置统计的时间(多少秒内在线人数)
$timeoutseconds = 300;取当前时间。
$timestamp = time();上面的完整代码:
<?php
$server = "localhost"; //your server
$db_user = "root"; //your mysql database username
$db_pass = "password"; //your mysql database password if any
$database = "users"; //the db name
$timeoutseconds = 300;//timeoutseconds limit
//get the current time
$timestamp = time();
//calculate the lowest timestamp allowed
$timeout = $timestamp-$timeoutseconds;
?>连接mysql
mysql_connect('localhost', 'username', 'password');也允许使用变量形式。
mysql_connect($server, $db_user, $db_pass);如果mysql数据库没有密码的话可以使用下面代码连接
mysql_connect($server, $db_user);查询数据库的代码:
mysql_db_query('database', 'query');我们只要有访客就要增加一条记录。
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
如果用户用错误信息的话,这样处理。
if(!($insert)) {
print "Useronline Insert Failed > ";
}然后实现当超过设置的时间就删除该用户记录。
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");同样给出删除记录出错的处理。
if(!($delete)) {
print "Useronline Delete Failed > ";
}下面我们解决数据库中不同IP的问题
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");使用
mysql_num_rows(query);来统计用户,代码如下。
$user = mysql_num_rows($result);最后关闭数据库。
mysql_close();显示在线的人数。
if($user == 1) {
print("1 user online\n");
} else {
print("$user users online\n");
}最终把上面代码写成一个PHP文件如下。
<?php
//Put your basic server info here
$server = "localhost"; //normally localhost
$db_user = "root"; //your MySQL database username
$db_pass = "password"; //your MySQL database password
$database = "users";
$timeoutseconds = 300; //it will delete all people which haven't refreshed(so probbably are
// offline or inactive) in $timieoutseconds time (so it actually checks the people that are active in the last
// $timeoutseconds seconds)
//this is where PHP gets the time
$timestamp = time();
//counts the timeout, all people which have been seen last online in earlier than this timestamp, will get removed
$timeout = $timestamp-$timeoutseconds;
//connect to database
mysql_connect($server, $db_user);
//add the timestamp from the user to the online list
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')");
if(!($insert)) {
print "Useronline Insert Failed > ";
}
//delete the peoples which haven't been online/active in the last $timeoutseconds seconds.
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
if(!($delete)) {
print "Useronline Delete Failed > ";
}
//select the amount of people online, all uniques, which are online on THIS page
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='".$_SERVER['PHP_SELF']."' ");
if(!($result)) {
print "Useronline Select Error > ";
}
//Count the number of rows = the number of people online
$user = mysql_num_rows($result);
//spit out the results
mysql_close();
if($user == 1) {
print("1 user online\n");
} else {
print("$user users online\n");
}
?>

 

 


<?php
class conn{
function __construct(){
  require("config.php");
  $conn = @mysql_connect($host,$root,$pass);
  @mysql_select_db($db);
  if(!$conn){
   echo "无法连接".mysql_errno() . ":" . mysql_error() ;
   exit;
  }
}
function query($sql){
  $result = @mysql_query($sql);
  if (!$result) {
   echo mysql_errno().":".mysql_error();
   exit;
  }
  return $result;
}
function next($result){
  return  @mysql_fetch_array($result);
}
function count_row($result){
  $row = @mysql_num_rows($result);
  return $row;
}
function close($result){
  @mysql_free_result($result);
  @mysql_close();
}
}
class fenye extends conn{
public $sql;
public $page;
public $countpage;
public $pagesize;
public $result;
function __construct($sql,$page,$pagesize){
  $this->sql = $sql;
  $this->page = $page;
  $this->countpage = $countpage;
  $this->pagesize = $pagesize;
  parent::__construct();
  $result = parent::query($sql);
  $num = parent::count_row($result);
  $this->countpage = ceil($num/$pagesize);
  $a = ($page-1)*$pagesize;
  $limit = "limit $a,$pagesize";
  $this->sql = $sql.$limit;
  $this->result = parent::query($this->sql);
}
function getlimit(){
  return $this->sql;
}
    function next(){
     return parent::next($this->result);
    }
function foor(){
  $a = $this->page -1;
  $b = $this->page +1;
  if ($this->page == 1) {
   echo "首页"."    ";
  }else  echo "<a href='?page=1'>首页</a>"."    ";
  if($this->page >1)
     echo "<a href='?page=$a'>上页</a>"."    ";
  else  echo "上页    ";
  for ($i =1;$i <= $this->countpage;$i++){
   echo "<a href='?page=$i'>$i</a>"."       ";
  }
  if($this->page < $this->countpage)
      echo "<a href='?page=$b'>下页</a>"."    ";
  else  echo "下页    ";
  if ($this->page == $this->countpage) {
   echo "末页"."    ";
  }else   echo "<a href='?page=$this->countpage'>末页</a>";
  #######################
 
}
}
####################
if (isset($_GET['page'])) {
$page = $_GET['page'];
}else  $page = 1;
$sql = "select * from guestbook ";
$pagesize =3;
$p = new fenye($sql,$page,$pagesize); 
$sql = $p->getlimit();
$result = $p->query($sql);
while($row = $p->next()){
echo $row['id']."<br>";
}
$p->foor();
$p->close($result);
?>

 

/************/
config.php
/***********/
<?php
    $host = 'localhost';            // 数据库服务器
    $user = 'root';            // 数据库用户名
    $password = '123456';            // 数据库密码
    $db = 'project';            // 数据库名

//打开数据库
$conn = mysql_pconnect ($host,$user,$password);
if(!$db)
{
die('<b>数据库连接失败!</b>');
}
//选择数据库
mysql_select_db ($db);
?>
/*********************/
index.php
/********************/

<html>
<body>
<script type="text/javascript">
function check_form(obj){
    if(document.getElementById('post_name').value==''){
        alert('写一下您的大名吧');
        document.getElementById('post_name').focus();
        return false;
    }
    if(document.getElementById('post_body').value==''){
        alert('不是来捣乱的吧,写一下留言内容吧!');
        document.getElementById('post_body').focus();
        return false;
    }
    return true;

}
</script>
        <form action="/sent.php" method="post" onSubmit="return check_form(this)">
      <table width="100%"  border="0">
        <tr>
          <td align="right">您的大名</td>
          <td><input name="post_name" id="post_name" type="text" size="20" maxlength="20"></td>
        </tr>
                <td align="right">您的发言</td>
<td><textarea name="post_body" id="post_body" rows="10" cols="50">您的发言</textarea></td>
        <tr>
          <td align="right"> </td>
          <td><input type="submit" name="Submit" value="提        交" ></td>
        </tr>
      </table>
    </form></td>
        </body>
        <html>
<?php
    include('get.php')
?>
/***************************/
get.php
/****************************/
[quote]
<?php
include('config.php');
$query = "SELECT * FROM project_post";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)) echo "姓名:$row[post_name] </br> 内容: $row[post_body]</br>";
?>
/***************************/
sent.php
/***************************/

<?php
include('config.php');
$post_name=addslashes($_POST['post_name']);
$post_body=addslashes($_POST['post_body']);
$sql = "INSERT INTO project_post (post_name,post_body) VALUES('$post_name','$post_body')";
mysql_query($sql);
header('location:/index.php');
?>

 

Apache Rewrite实例2007-03-04 11:25<VirtualHost *:80>
    ServerAdmin host@discuz.com
    DocumentRoot D:/www
    ServerName www.xiaojia.com
    ServerAlias xiaojia.com xiaojia.net
    ErrorLog D:/www/logs/xiaojia.com-error_log
    CustomLog D:/www/logs/xiaojia.com-access_log%Y%m%d combined
#    Alias /upimg "/home/www/wwwroot/phpchina.cn/upimg/"
#    Alias /wiki "/home/www/wwwroot/phpchina.cn/wiki/"
    <IfModule mod_rewrite.c>
    RewriteEngine On
#    RewriteCond %{HTTP_HOST} !111cn.net [OR]
#    RewriteCond %{HTTP_HOST} !www.111cn.net
#    RewriteRule ^(.*)$ http://www.111cn.net$1 [R=301,L]
    RewriteRule ^(.*)/archiver/([a-z0-9\-]+\.html)$ $1/archiver/index.php?$2
    RewriteRule ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3
    RewriteRule ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page\%3D$4&page=$3
    RewriteRule ^(.*)/profile-(username|uid)-(.+)\.html$ $1/viewpro.php?$2=$3
    </IfModule>

</VirtualHost>
[!--infotagslink--]

相关文章

  • python实现学生通讯录管理系统

    这篇文章主要为大家详细介绍了python实现学生通讯录管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-25
  • BootStrap栅格系统、表单样式与按钮样式源码解析

    这篇文章主要为大家详细解析了BootStrap栅格系统、表单样式与按钮样式源码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2017-01-23
  • 详解为什么现代系统需要一个新的编程模型

    如今高要求的分布式系统的建造者遇到了不能完全由传统的面向对象编程(OOP)模型解决的挑战,但这可以从Actor模型中获益。...2021-05-20
  • 护卫神 主机管理系统使用说明(MSSQL管理)

    护卫神·主机管理系统该版本支持在Windows Server 200320082012,含32位和64位,直接开设配置WEB站、FTP站,以及SQL Server和MySQL,是您开设和管理虚拟主机的绝好帮手。但是对于新用户可能在使用上有一些困难,因此请仔细阅读如下说明文档...2016-01-27
  • 利用C#修改Windows操作系统时间

    这篇文章主要介绍了利用C#修改Windows操作系统时间,帮助大家更好的利用c#操作系统,感兴趣的朋友可以了解下...2020-12-08
  • vivo OriginOS新系统如何更新 originos系统更新方法

    vivo新系统更新的步骤是什么?如何更新到vivo的最新系统?vivo的最新系统太亮眼了,不少vivo的用户都在跃跃欲试想体验一下最新的系统。vivo新系统虽然做出来了不过我们想体验的话还是要等待一段时间。到时大家通过下面的方法就可以使用到新系统了...2020-12-08
  • C#实现影院售票系统

    这篇文章主要为大家详细介绍了C#实现影院售票系统,解析了售票系统的难点,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • 基于jquery实现彩色投票进度条代码解析

    这篇文章主要介绍了基于jquery实现彩色投票进度条代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-08-27
  • ColorOS7.2好不好用 ColorOS7.2系统升级体验

    ColorOS7.2系统怎么样?好不好用?值不值得升级?下面小编带来ColorOS7.2系统升级体验...2020-06-29
  • C#多线程编程中的锁系统(四):自旋锁

    这篇文章主要介绍了C#多线程编程中的锁系统(四):自旋锁,本文讲解了基础知识、自旋锁示例、SpinLock等内容,需要的朋友可以参考下...2020-06-25
  • Unity实现换装系统

    这篇文章主要为大家详细介绍了Unity实现换装系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-04-11
  • php需登录的文件上传管理系统

    本文给大家介绍一个不错的需要登录的php 文件上传管理系统,功能简单有需要了解的同学可参考。 代码如下<&#63;php$admin_pw="admin";//管理密码$uploaddir="upload";//上传目录session_start();if($_GET['action']=="g...2015-10-30
  • C++学生信息管理系统

    这篇文章主要为大家想详细介绍了C++学生信息管理系统的实现代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • PHP再线投票connect.php

    <?php /* **数据库连接 */ $connect = new PDO ("mysql:host=localhost;dbname=online","root","960515"); ?> < PHP (Hypertext...2016-11-25
  • C语言学生成绩管理系统小设计

    这篇文章主要为大家详细介绍了C语言学生成绩管理系统小设计,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • C#毕业设计之Winform零压健身房管理系统

    本文介绍了个人的《零压健身房管理系统(扁平化)》的基本流程和功能点的介绍,虚心接受各位的意见,欢迎在提出宝贵的意见,大家一起探讨学习...2021-09-26
  • .NET Core如何获取操作系统中的各种信息

    .net core是最近讨论频率很高的话题,下面这篇文章主要给大家介绍了关于利用.NET Core如何获取操作系统中各种信息的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧...2021-09-22
  • C++顺序表实现图书管理系统

    这篇文章主要为大家详细介绍了C++顺序表实现图书管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-10-13
  • 基于python实现银行管理系统

    这篇文章主要介绍了基于python实现银行管理系统,文中有非常详细的代码示例,对正在学习python项目制作的小伙伴们有很好的帮助,需要的朋友可以参考下...2021-04-19
  • C#实现操作windows系统服务(service)的方法

    这篇文章主要介绍了C#实现操作windows系统服务(service)的方法,可实现系统服务的启动和停止功能,非常具有实用价值,需要的朋友可以参考下...2020-06-25