一 代码
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="css/in.css">
<title>防止页面刷新</title>
</head>
<body>
<div id="one">
<div id="two">
<span class="one">防止页面刷新</span><br><br>
<span class="two">
<?php
if($_SESSION['id']==$_POST['id']){//判断session变量是否与获取的id值相同
if(isset($_POST['id'])){// 当页面中存在id变量时
if($_POST['text']!=""&&$_POST['pwd']!=""){// 判断文本框中是否存在内容
if(!is_dir('txt')){//如果目录不存在
mkdir('txt');// 创建文件夹
}
$str="用户名:".$_POST['text']."<br>密码:".$_POST['pwd'];// 拼接字符串
if(file_put_contents('txt/in.txt',$str)>0){// 如果向文件中写入数据成功
echo "内容成功提交";
}
}else{
echo "文本框内容不能为空";
}
}
}else{
echo "页面已经设置防刷新设置";
}
$id=microtime();// 定义当前时间微秒数
$_SESSION['id']=$id;// 将微秒数存到session中
?>
</span>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id;?>">
用户名:<input type="text" name="text" class="three"><br>
密 码:<input type="password" class="three" name="pwd"><br>
<input class="four" class="clickFour" type="submit" name="sub" value="提交">
<input class="four"type="reset" name="sub" value="重置">
</form>
</div>
</div>
</body>
</html>
二 运行结果
提交后,再刷新页面(F5),运行结果如下: