login.php
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>用户登录</title> </head> <body> <form action="demo1.php" method="post"> 用户名:<input type="text" name="username"/><br> 密 码:<input type="password" name="password"/><br> <input type="submit" name="login" value="提交"/> </form> </body> </html>
demo1.php
<?php session_start(); // 使用session回话必须调用session_start() /* * 获取登录名和密码并保持到session中 */ // 用户是否提交 // 是否为空值 // 不为空,保存到session中 if(isset($_POST['login'])){ $username = $_POST['username']; $password = $_POST['password']; if($username != "" && $password != ""){ $_SESSION['username'] = $username; $_SESSION['password'] = $password; echo "<script type='text/javascript'>"; echo "location.href='result.php'"; echo "</script>"; }else { echo "<script type='text/javascript'>"; echo "location.href='login.php'"; echo "</script>"; } }else { echo "<script type='text/javascript'>"; echo "location.href='login.php'"; echo "</script>"; } ?>
result.php
<?php session_start(); header('Content-Type:text/html;charset=utf-8'); echo "用户名:".$_SESSION['username']."<br/>"; echo "密 码:".$_SESSION['password']."<br/>"; /* unset() 和session_destory()使用 * * unset() 销毁一个session对象 * session_destory 销毁所有的session对象 * * */ unset($_SESSION['username']); echo isset($_SESSION['username']).'<br/>'; echo isset($_SESSION['password']); ?>
保
// cookie与session的区别 // cookie与session主要的区别在于:cookie主要保存在客户端,而session保存/在服务器端。cookie一般用户保存登录名或购物车比较多,而session用户保存后台管理员的比较多