cookies小实例
if(isset($_GET['out'])) { //用于注销cookies setcookie('id',""); setcookie('pass',""); echo "<script>location.href='login.php'</script>"; //因为cookies不是及时生效的,只有你再次刷新时才生效,所以,注销后让页面自动刷新。 } if(isset($_POST['name'])&&isset($_POST['password'])) //如果变量用户名和密码存在时,在下面设置cookies { //用于设置cookies setcookie('id',$_POST['name'],time()+3600); setcookie('pass',$_POST['password'],time()+3600); echo "<script>location.href='login.php'</script>"; //让cookies及时生效 } if(isset($_COOKIE['id'])&&isset($_COOKIE['pass'])) { //cookies设置成功后,用于显示cookies echo "登录成功!<br />用户名:".$_COOKIE['id']."<br/>密码:".$_COOKIE['pass']; echo "<br />"; echo "<a href='login.php?out=out'>注销cookies</a>"; //双引号内,如果再有引号,需要用单引号。 } ?> <form action="" method="post"> 用户ID: <input type="text" name="name" /><br/><br/> 密码: <input type="password" name="password" /><br/><br /> <input type="submit" name="submit"> </form>
session小实例
<?php //session用法实例 session_start();//启动session,必须放在第一句,否则会出错。 if(isset($_GET['out'])) { unset($_SESSION['id']); unset($_SESSION['pass']); } if(isset($_POST['name'])&&isset($_POST['password'])) { //用于设置session $_SESSION['id']=$_POST['name']; $_SESSION['pass']=$_POST['password']; } if(isset($_SESSION['id'])&&isset($_SESSION['pass'])) { echo "登录成功!<br/>用户ID:".$_SESSION['id']."<br />用户密码:".$_SESSION['pass']; echo "<br />"; echo "<a href='login.php?out=out'>注销session</a>"; } ?> <form action="login.php" method="post"> 用户ID: <input type="text" name="name" /><br/><br/> 密码: <input type="password" name="password" /><br/><br /> <input type="submit" name="submit"> </form>