登录界面:logon.php
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>用户登录</title> </head> <?php //用户是否保持用户名和密码 if(isset($_COOKIE['username'])){ echo "<script language='javascript'>"; echo "location.href='login_success.html'"; //重定向url echo "</script>"; } ?> <body> <form action="deal_cookie.php" method="post"> 用户名:<input type="text" name="username"/><br> 密 码:<input type="password" name="password"/><br> <input type="checkbox" name="save" id="save"/><label for="save">7天内自动登录</label><br> <input type="submit" name="login" value="提交"/> </form> </body> </html>
cookie处理:deal_cookie.php
<?php //设置编码 header('Content-Type:text/html;charset=utf-8'); // 判断用户是否提交 if(isset($_POST['login'])){ //获取用户名和密码 $username = $_POST['username']; $password = $_POST['password']; if($username !="" && $password != ""){ // 用户是否保存登录名 if(isset($_POST['save'])){ // 创建cookie,保存7天 setcookie('username',$username,time()+(7*24*60*60)); setcookie('password',$password,time()+(7*24*60*60)); } // 重定向用户 header('Location:login_success.html'); }else{ echo "<script type='text/javascript'>alert('用户名和密码不能为空');history.back;</script>"; } } ?>
成功登录:login_success.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录成功</title> </head> <body> <h1>登录成功</h1> </body> </html>