PHP学习笔记——表单数据获取,Session,Cookie

来源:互联网 发布:关键词优化报价 编辑:程序博客网 时间:2024/06/04 20:14

演示代码如下:

一. 表单页面

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>
 <form action="Demo12.php" method="post">
  Name:<input type="text" name="username"/></br>
  Age:<input type="text" name="age"/></br>
  Email:<input type="text" name="email"/></br>
  Password:<input type="password" name="pwd"/></br>
  <input type="submit" name="send" value="提交"/>
 </form>
</body>
</html>

二 数据处理页面

<?php
header("Content-type: text/html; charset=GBK");
$username=$_POST['username'];
if(!isset($_POST["send"]) || $_POST["send"] !='提交'){
 header("Location:Demo13.php");
 exit;
}else{
 $username=$_POST['username'];
 // $password=$_POST["pwd"];
 $password=$_COOKIE['password'];
 $email=$_POST["email"];
 $age=$_POST["age"];
 if(strlen($password)<5){
  echo "<script>alert('密码长度不能小于5');history.back()</script>";
  exit;
 }else{
  setcookie("username",$username,time()+7*224*60*60);
  setcookie("password",$password,time()+7*224*60*60);
  session_start();
  $_SESSION['password']=$password;
  header("Location:Demo14.php");
 }

}
?>

三,处理成功,跳转从Cookie,Session中获取数据,展现

<?php
header("Content-type: text/html; charset=GBK");
session_start();
echo "重Cookie中获取的用户名:".$_COOKIE["username"];
echo "从Session中获取的密码:".$_SESSION["password"];
echo "从Cookie中获取的密码:".$_COOKIE["password"];


?>

 

 

原创粉丝点击