4.19.php
<?php /* * * @Authors peng--jun * @Email 1098325951@qq.com * @Date 2015-11-05 12:10:47 * @Link http://www.cnblogs.com/xs-yqz/ * @version $Id$ ========================================== */ header("Content-type: text/html; charset=UTF-8"); if ($_COOKIE["user"]["xm"]) { //尝试获取指定的Cookie变量,如果有 $visnum = intval($_COOKIE["user"]["num"])+1;//将原来的访问次数加1 $expire = intval($_COOKIE["user"]["expire"]);//获取有效期 //将本次访问时间写入Cookie setcookie("user[dt]",date("Y-m-d h:i:s"),time()+3600*$expire); setcookie("user[num]",$visnum,time()+3600*$expire); echo "欢迎您:".$_COOKIE["user"]["xm"]; echo "<br/>这是您第".$visnum."次访问本网站"; echo "<br/>您上次访问是在".$_COOKIE["user"]["dt"]; }else {//没有Cookie则显示登录表单 echo ' <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <form method="post" action="4.20.php"> <p>账号:<input name="xm" type="text"></p> <p>密码:<input name="pwd" type="password"></p> <p>保存:<select name="Save"> <option value="-1">不保存</option> <option value="7">保存一周</option> <option value="30">保存一月</option> <input type="submit" name="submit" value="登陆"> </p> </form> </body> </html> '; } ?>
4.20.php
<?php /* * * @Authors peng--jun * @Email 1098325951@qq.com * @Date 2015-11-05 19:04:51 * @Link http://www.cnblogs.com/xs-yqz/ * @version $Id$ ========================================== */ header("Content-type: text/html; charset=UTF-8"); if ($_POST["xm"] == "admin" && $_POST["pwd"] == "123") { setcookie("user[xm]",$_POST["xm"],time()+3600*intval($_POST["Save"])); setcookie("user[dt]",date("Y-m-d h:i:s"),time()+3600*intval($_POST["Save"])); setcookie("user[num]",1,time()+3600*intval($_POST["Save"])); setcookie("user[expire]",$_POST["Save"],time()+3600*intval($_POST["Save"])); //保存有效期到Cookie echo $_POST["xm"].":首次光临"; var_dump($_COOKIE); }else{ echo "<script>alert('用户或密码不正确');location.href='4.19.php';</script>"; } ?>