登陆小demo
<?php if( isset($_COOKIE['username']) && isset($_COOKIE['auth'])){ $arr = explode(':',$_COOKIE['auth']); $id = end($arr); $link = mysqli_connect('127.0.0.1','root','root','xmz',8889) or die("连接失败"); mysqli_set_charset($link,'utf8'); $sql = "select id,username,password from member where id ={$id} "; $res = mysqli_query($link,$sql); if(mysqli_num_rows($res)){ $row = mysqli_fetch_assoc($res); $authStr = md5($row['username'].$row['password'].'xmz').":".$id; if($authStr == $_COOKIE['auth']){ exit("<script> alert('正在登陆中,请稍后'); location.href = 'shouye.php'; </script>"); } }} ?> <!doctype html> <html lang="en"> <head> <title>Bootstrap 实例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <h3 class="text-center"> 登陆页面 </h3> <form role="form" method="post" action="doLogin.php"> <div class="form-group"> <label for="exampleInputEmail1">请输入用户名</label><input type="text" class="form-control" id="username" name="username" /> </div> <div class="form-group"> <label for="exampleInputPassword1">请输入密码</label><input type="password" class="form-control" id="password" name="password"/> </div> <div class="checkbox"> <label><input type="checkbox" name="autoLogin" value="1"/>一周内自动登陆</label> </div> <button type="submit" class="btn btn-default">登陆</button> </form> </div> </div> </div> </body> </html>
<?php $username = $_POST['username']; $password = md5($_POST['password']); $autoLogin = isset($_POST['autoLogin'])?1:0; $link = mysqli_connect('127.0.0.1','root','root','xmz',8889) or die("数据库连接失败"); mysqli_set_charset($link,'utf8'); $sql = "SELECT id,username, password FROM member WHERE username = '{$username}' and password = '{$password}'"; $res = mysqli_query($link,$sql); $num = mysqli_num_rows($res); if($num){ $row = mysqli_fetch_assoc($res); if($autoLogin == 1){ setcookie('username',$username,strtotime('+7 days')); $salt = 'xmz'; $auth = md5($username.$password.$salt).":".$row['id']; setcookie('auth',$auth,strtotime('+7 days')); exit("<script> alert('登陆成功'); location.href = 'shouye.php'; </script>"); }else{ setcookie('username',$username); $salt = 'xmz'; $auth = md5($username.$password.$salt).":".$row['id']; setcookie('auth',$auth); exit("<script> alert('登陆成功'); location.href = 'shouye.php'; </script>"); } }else{ exit("<script> alert('密码错误'); location.href = 'login.php'; </script>"); }
<?php if(!isset($_COOKIE['username']) && (!isset($_COOKIE['auth']))){ exit("<script> alert('请先登陆'); location.href = 'login.php'; </script>"); } $arr = explode(':',$_COOKIE['auth']); $id = end($arr); $link = mysqli_connect('127.0.0.1','root','root','xmz',8889) or die("连接失败"); mysqli_set_charset($link,'utf8'); $sql = "select id,username,password from member where id ={$id} "; $res = mysqli_query($link,$sql); if(mysqli_num_rows($res)){ $row = mysqli_fetch_assoc($res); $authStr = md5($row['username'].$row['password'].'xmz').":".$id; if($authStr != $_COOKIE['auth']){ exit("<script> alert('校验失败'); location.href = 'login.php'; </script>"); } }else{ exit("<script> alert('请先登陆'); location.href = 'login.php'; </script>"); } ?> <!doctype html> <html lang="en"> <head> <title>Bootstrap 实例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body><div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <nav class="navbar navbar-default" role="navigation"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button> <a class="navbar-brand" href="#">Brand</a> </div> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li class="active"> <a href="#">Link2</a> </li> <li> <a href="#">Link</a> </li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown<strong class="caret"></strong></a> <ul class="dropdown-menu"> <li> <a href="#">Action</a> </li> <li> <a href="#">Another action</a> </li> <li> <a href="#">Something else here</a> </li> <li class="divider"> </li> <li> <a href="#">Separated links</a> </li> <li class="divider"> </li> <li> <a href="#">One more separated link</a> </li> </ul> </li> </ul> <form class="navbar-form navbar-left" role="search"> <div class="form-group"> <input type="text" class="form-control" /> </div> <button type="submit" class="btn btn-default">Submit</button> </form> <ul class="nav navbar-nav navbar-right"> <li> <a href=""> 欢迎您 <?php echo $_COOKIE['username']; ?></a> </li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown<strong class="caret"></strong></a> <ul class="dropdown-menu"> <li> <a href="#">Action</a> </li> <li> <a href="#">Another action</a> </li> <li> <a href="#">Something else here</a> </li> <li class="divider"> </li> <li> 欢迎您 </li> </ul> </li> </ul> </div> </nav> </div> </div> </div></body> </html>