好了,基础的东西已经学完,让我们来进行实战吧!大家先下这个范例程序。然后可以在自己的平台上先试试看。相信这样会得到一点感性认识。
在这个应用中一共使用了三个文件vote.php,config.php,1.gif以及一个保存数据的文件(该文件的名字可以自由设定,在这里我们设为sum.txt),其中vote.php是主程序文件,config.php则用于设置一些经常需要修改的信息。
//config.php文件
//title变量设定本html档中的title标记,也就是在浏览器标题栏里出现的标题
$title=读者类型调查表;
//设定调查内容。这里用了一个数组,注意用""号把内容括起来
$option= array("学生","工人","农民","知识分子","资本家","流氓");
//设定调查结果保存到哪个文件中去
$countfile = "sum.txt";
//设定同一机器可以再次投票的时间
$limitdate = time()+60*60*24*365;
?>
// vote.php文件
/*首先将配置信息装进来,这里我简单说明一下 require和include的区别,require通常放在PHP 程序的最前面,PHP 程式在执行前,就会先读入 require 所指定引入的档案,使它变成 PHP 程式网页的一部份。常用的函式,亦可以这个方法将它引入网页中。 include 则一般是放在流程控制的处理区段中。PHP 程式网页在读到 include 的档案时,才将它读进来。这种方式可以把程式执行时的流程简单化。 */
config.php";?>
/*下面这部分就是cookie了,它的expire时间在上面的config.php中设置,另外大家可能已经发现了,在这里有两个 if比较语句,第一个是检测是否已经有cookie设置,第二个则是为了避免人家还没有投票程序就把cookie发出去。*/
if (isset($vote) && $vote=="Ready") $status = 1;
else $status=0;
if ($ready=="true")
{
setcookie("vote","Ready",$limitdate);
}
?>
charset=gb2312">
bgcolor="#FFFFFF">
//读出原来的选票结果,赋给变量$result
if (file_exists ($countfile)){
$result = file($countfile);
}
?>
/*下面是主程序 用switch做了三个分支,Case 1、查看结果;Case 2、提交选择;Case 3、生成表 单*/
switch ($elect) {
/* 查看结果,实际就是把上面用file()函数读出来的变量显示出来*/
case "查看结果":
echo "";
for($i=0;$ii++){
echo "tr>"."".$option[$i]."";
echo ""."img src='1.gif' width='";
echo "$result[$i]*10";
echo "' height='8'> ";
echo ""." ".$result[$i]."tr>";
}
echo "";
break;
/*把选择结果写入到文件中去,注意要用flock把文件锁住!虽然对投票程序来说这个问题不是特别 突出*/
case "提交":
if ($status == 1){
echo "你已经投过票了";
}elseif($sugest == 0){
echo "you havn't choice";
}else{
if($fp=fopen($countfile,"w")){
if(flock($fp,3)){
for ($i=0; $ii++){
if ($sugest == $i+1){
$result[$i]=$result[$i]+1;
}else $result[$i]=$result[$i] + 0;
echo $option[$i]." ".$result[$i]."br>";
fwrite($fp,$result[$i]." ");
}
}else "投票失败";
}else echo "投票失败";
fclose($fp);
}
break;
/*显示投票的表单*/
default:
echo "vote.php' method='post' name='form1'>";
$sum = count($option);
for ($i=0; $ii++){
echo "sugest' value=";
echo $i+1;
if ($i==0) echo " CHECKED";
echo ">".$option[$i]."br>";
}
echo "";
echo "br>";
echo "提交'>";
echo "查看结果'>";
echo "";
}
?>
这样一个简单的php的投票应用就完成了,说它简单是因为这个程序只完成了投票应用的几个最重要的功能,还有一些细节问题没有去处理。比如文件写失败之后的处理,用户关闭cookie功能的应付办法等等。具体的完善就留给读者朋友来解决了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6
Visual web development tools
