⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 select.cgi

📁 XYZ投票统计系统1.5C版
💻 CGI
字号:
#!/usr/bin/perl
###########################################################################
#CGI脚本名称:XYZ投票统计系统1.5C版,用于WWW网站建立投票调查统计系统。
#Copyright (C) 2000 Yu Ning Chen
#
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#############################################################################

$url='select/';        #设置存放记录的目录,用户自行设置
$select_cgi_url='http://zjxyz/cgi-bin/select.cgi'; #用户自行设置
$imgurl='http://zjxyz/images/line.gif';        #用户自行设置
$filelock=0; #是否需要在读写文件时对文件锁定,一般在UNIX平台上使用,目前在windows下要设置为0,否则不能正常运行。

# # # # # 处理环境参数 # # # #
$buffer=$ENV{'QUERY_STRING'};
@pairs = split(/&/, $buffer);
foreach $pair (@pairs){
   ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $A{$name} = $value;}
#get files path of the id
 $selects="$url$A{'ID'}_select.txt";
 $countrecord="$url$A{'ID'}_count.txt";
 $iprecord="$url$A{'ID'}_iprecord.txt";
 $selectconf="$url$A{'ID'}_selectconf.txt";
#------------ START main part ---------------
open(RE,"$countrecord");
if ($filelock){flock(RE,1);}
  $count=<RE>;
if ($filelock){flock(RE,8);}
close(RE);
if ($count eq '' ){
   $count=0}

print"Content-type:text/html\n\n";

############  分支判断  #############

if ($A{B3} eq 'B3'){     #如果调用CGI的参数有一个是B3=B3,CGI则在网页上显示已投票人数
   print "document.write('共有'+$count+'个人投了票');";
   exit 0;}
   &readdata;

if ($A{B3} eq 'easymode'){
     &easymode;
     exit 0;}

if ($A{B2} ne ''){#检验是否按下的是结果的按钮,是的话直接去显示结果
   &dsp;
   exit 0;}

if ($A{B1} ne ''){#检验是否按下的是投票的按钮
   &writeselect;
   exit 0;}

#------------ END main part ---------------
sub writeselect{

# IP LOCK
($sec,$min,$hour,$mday,$mon,$year)=localtime(time); 
$mon=$mon+1;
$year=$year+1900;

 if($iplock&&&iplock){
    
   print"谢谢,根据IP显示您在短时间内已经投了一次票,请下次再来!";
    &dsp;
    exit 0;}

$count++;   #按下了投票按钮投票人数加1
open(WR,">$countrecord");
if ($filelock){flock(WR,2);}

  print WR $count;
if ($filelock){flock(WR,8);}
close(WR);

for ($j=0;$j<$i;$j++){
    $form[$j]=~tr/a-z/A-Z/;
      if ($A{$form[$j]} eq $form[$j]){
           $count[$j]++;
           $sum++}# end if
}# end for  


open (WR,">$selects");
if ($filelock){flock(WR,2);}

  for($j=0;$j<$i;$j++){
   print WR "$form[$j],$name[$j],$count[$j],\n";}
if ($filelock){flock(WR,8);}
close(WR);
&dsp;
exit 0;
}

#############################################################################
############         显示数据   模块    sub  dsp           ##################
##############################3##############################################

sub dsp{
print '<style><!--A:link{text-decoration:none;color:#0000FF}A:visited{text-decoration:none;color:#0000FF}A:hover{text-decoration:none;color:red;}body{font-family:"新明细体";font-size:9pt;}td{font-family:"新明细体";font-size:9pt;}--></style>';
print "<title>$title投票结果</title>";#弹出的小窗口的标题,没有这一句的话标题会是一连串调用CGI的的参数
print "<body bgcolor=#FFFFFF>";
print '<script language="JavaScript">';#生成javascript对数据进行表格输出

print "document.write('<div align=center><font color=#400080>$title投票结果</font><br>共有<font color=red>'+$count+'</font>个人投了票/总票数:<font color=red>$sum</font></div>');";
print "document.write('<table border=0 cellpadding=0 cellspacing=1 width=100%>');";

print "document.write('<tr><td width=148 align=center bgcolor=#BBDDFF>投票项</td><td width=40 align=center bgcolor=#BBDDFF>票数</td><td align=center bgcolor=#BBDDFF  colspan=2>百分比</td></tr>');";
if ($sum==0){$sum=1;}
print "imgurl='$imgurl';sum=$sum;i=$i;c=new Array($i);n=new Array($i);"; 
for ($j=0;$j<$i;$j++){
 print "c[$j]=$count[$j];n[$j]='$name[$j]';";}

print "for(j=0;j<i;j++){p=(c[j]/sum)*10000;p=(Math.round(p)/100);";

print "document.write('<tr><td  width=148 bgcolor=#FBE1C6>'+n[j]+'</td><td width=40 align=center bgcolor=#FBE1C6>'+c[j]+'</td>');";

print "if (navigator.appName == 'Microsoft Internet Explorer'){";
print "document.write('<td bgcolor=#FBE1C6><img src='+imgurl+' width='+p+'% height=6></td><td bgcolor=#FBE1C6 width=40 align=right>'+p+'%</td></tr>');}";

print "if (navigator.appName == 'Netscape'){";
print "t=1+p;";
print "document.write('<td bgcolor=#FBE1C6><img src='+imgurl+' width='+t+'  height=6></td><td bgcolor=#FBE1C6 width=40 align=right>'+p+'%</td></tr>');}";

print '}</script></table></center></div>';
print "<div align=right><br><a href=javascript:this.close();>关闭窗口</a></div>";
print '<div align=center><br>该投票系统由<a href="http://zjxyz.yeah.net" target="_blank">CGI编程设计室</a>制作,<br>你可以在该网站免费得到本CGI程序。</div>';
}

#############################################################################
############            IP锁定  模块    sub  iplock        ##################
##############################3##############################################

sub iplock{
$check=0;
open (FILE,"$iprecord");
if ($filelock){flock(FILE,1);}
@LINES=<FILE>; 
if ($filelock){flock(FILE,8);}
close(FILE);
$SIZE=@LINES; 
open (FILE,">$iprecord");
if ($filelock){flock(FILE,2);}

 for ($f=0;$f<$SIZE;$f++){
     $_=$LINES[$f];
     ($addr,$come_hour,$come_min,$come_sec,$come_time)=split(/,/,$_);
     $flag=($hour-$come_hour)*3600+($min-$come_min)*60+($sec-$come_sec);
    if( $addr eq "$ENV{'REMOTE_ADDR'}" ){
      print FILE "$ENV{'REMOTE_ADDR'},$hour,$min,$sec,$come_time\n";
      $check=1;
    }
    
    elsif ($flag < 120 && $flag > 0) {
     print FILE $_;
    }
 }#end for 
 if ($check == 0){
   print FILE "$ENV{'REMOTE_ADDR'},$hour,$min,$sec,$hour:$min:$sec\n";
if ($filelock){flock(FILE,8);}
   close(FILE);
   return 0;
 }

return 1;
if ($filelock){flock(FILE,8);}
close(FILE); 
}

#############################################################################
############          简单模式  模块  sub easymode         ##################
##############################3##############################################

sub easymode{

if ($sm=~/mu/i){
    $type='checkbox';
    $sw1='name';
    $sw2='value=ON'; 
}
 else{
    $type='radio';
    $sw1='value';
    $sw2='name=RR';}

print "document.write('<p align=center><font color=#7200E3>$title</font></p>');";
print "document.write('<form method=POST action name=$A{'ID'} ID=$A{'ID'}>');";

for ($j=0;$j<$i;$j++){
print "document.write('<input type=$type $sw1=$form[$j] $sw2>$name[$j]<br>');";}
print "document.write('<p align=center><input type=button value=投票 name=B1  onclick=$A{'ID'}_atshow();>');";
print "document.write('<input type=button value=结果 name=B2 onclick=$A{'ID'}_show();><br>');";
print "document.write('共有<font color=red>'+$count+'</font>个人投了票</p>');";
print "document.write('</form>');";

if ($newwin!~/old/){
    print "var $A{'ID'}_new_win=1;";}
else {
    print "var $A{'ID'}_new_win=0;";}
print "var $A{'ID'}=null;";
print "$A{'ID'}_len=document.$A{'ID'}.length-2;$A{'ID'}_Flag=1;";#plug poin
print "function $A{'ID'}_atshow(){if($A{'ID'}_Flag==0){";
print "alert('谢谢!你已经投过一次票了,请下次再来!');return 0;}";
print "a='$select_cgi_url?ID=$A{'ID'}&B1=B1&';$A{'ID'}_k=0;";
print "for(j=0;j<$A{'ID'}_len;j++){if(document.$A{'ID'}.elements[j].checked==true){";#plug poin
if ($type eq 'checkbox'){
  print "tem=document.$A{'ID'}.elements[j].name;$A{'ID'}_k=1;}";
  print "else tem='F';a+=document.$A{'ID'}.elements[j].name+'='+tem+'&';}";}
else {
  print "tem=document.$A{'ID'}.elements[j].value;$A{'ID'}_k=1;}";
  print "else tem='F';a+=document.$A{'ID'}.elements[j].value+'='+tem+'&';}";}
print "if($A{'ID'}_k==0){$A{'ID'}_Flag=1;alert('对不起,你还没有选择你的投票项!');return 0;}$A{'ID'}_Flag=0;";
print "if ($A{'ID'}_new_win==1){$A{'ID'}=window.open(a,'$A{'ID'}','width=350,height=400,resizable=1,scrollbars=yes');}";
print "else{this.location.href = a;}}";
print "function $A{'ID'}_show(){";
print "a='$select_cgi_url?B2=B2&ID=$A{'ID'}';";
print "if ($A{'ID'}_new_win==1){$A{'ID'}=window.open(a,'$A{'ID'}','width=350,height=400,resizable=1,scrollbars=yes');}";
print "else{this.location.href = a;}}";
}#end sub

#############################################################################
############      读取两个数据文件  模块    sub readdata   ##################
##############################3##############################################

sub readdata{ 
open (REconf,"$selectconf");# 读取设置文件 userID_selectconf.txt
if ($filelock){flock(REconf,1);}
$title=<REconf>;
$passwd=<REconf>;
$iplock=<REconf>;
$sm=<REconf>;
$newwin=<REconf>;
if ($filelock){flock(REconf,8);}
close (REconf);
if ($iplock=~/NO/i){
     $iplock=0;}
chop($title);
$sum=0;
$i=0;
open(RE,"$selects"); # 读取记录文件 userIDselect.txt
if ($filelock){flock(RE,1);}

 while($re=<RE>){
  ($form[$i],$name[$i],$count[$i])= split(/,/, $re);  
     $forms{$name[$i]}=$count[$i];  
    $sum+=$count[$i];
    $i++;
 }#end while

if ($filelock){flock(RE,8);}

close (RE);
}#end readdata sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -