Session防止页面刷新

一 代码

<?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>
					密&nbsp;&nbsp;码:<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),运行结果如下:

 

 

  • 大小: 4.3 KB
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值