cookie保存在客户端,session保存在服务器端,cookie目的可以跟踪会话,也可以保存用户喜好或者保存用户名密码
session用来跟踪会话
界面如图所示:
1.登录界面
实例
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>test</title> <style type="text/css"> body{ font-family: microsoft yahei; } .login{ background-color: #F5F5F5; width: 300px; border: 1px solid #000; border-radius: 2px; margin: auto; } .login span,p{ margin-left: 15px; } button{ margin-left: 120px; width: 60px; height: 25px; border: none; } </style> </head> <body> <div class="login"> <p> <label for="user">username:</label> <input type="text" name="user" id="user"> </p> <p> <label for="pass">password:</label> <input type="password" name="pass" id="pass"> </p> <p> <label for="record">remeber me</label> <input type="checkbox" name="record" id="record" checked> </p> <p> <button>login</button> </p> </div> </body> </html> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $('button:first').click(function () { if($('#user').val().length==0){ $('button:first').parent($('span').remove()) $('button:first').after('<p><span style="color: #AA1111;">username is request!</span></p>') $('#user').focus() }else if($('#pass').val().length==0){ $('button:first').parent($('span').remove()) $('button:first').after('<p><span style="color:#AA1111;">password is request!</span></p>') $('#pass').focus() }else { $('button:first').parent($('span').remove()) var record=0 if($('input[type=checkbox]').prop('checked')){ record=1 } $.ajax({ url:'check.php?m=login', type:'POST', dataType:'JSON', data:{ 'user':$('#user').val(), 'pass':$('#pass').val(), 'record':record }, success:function (msg,status,xhr) { if(msg['status']==0){ $('button:first').parent($('span').remove()) $('button:first').after('<p><span style="color:green;">success skiping...</span></p>') setTimeout(function () { $(window).attr('location','user.php') },1500) }else if(msg['status']==1){ $('button:first').parent($('span').remove()) $('button:first').after('<p><span style="color:#ff0000;">password error!</span></p>') setTimeout(function () { $('button:first').parent($('span').remove()) },1500) } } }) } }) }) </script>
运行实例 »
点击 "运行实例" 按钮查看在线实例
2. check.php
实例
<?php session_start(); if(!isset($_SESSION['user_id'])) { if ($_GET['m'] == 'login') { $conn = mysqli_connect('127.0.0.1', 'root', 'root', 'ttest') or die('failed'); $user = $_POST['user']; $pass = $_POST['pass']; $record = $_POST['record']; $query = "SELECT `user_id`,`userName` FROM `user` WHERE `userName`='{$user}' AND `userPass`='{$pass}'"; $res = mysqli_query($conn, $query); if (mysqli_num_rows($res) == 1) { if ($record == 1) { $row = mysqli_fetch_array($res); $_SESSION['user_id'] = $row['user_id']; $_SESSION['userName'] = $row['userName']; setcookie('user_id', $row['user_id'], time() + 3600); setcookie('userName', $row['userName'], time() + 3600); } echo json_encode([ 'status' => '0', 'reg_msg' => 'success' ]); } else { echo json_encode([ 'status' => '1', 'reg_msg' => 'error' ]); } } else { echo json_encode(['msg' => 'error!']); } }else{ header('Location:user.php'); }
运行实例 »
点击 "运行实例" 按钮查看在线实例
3.user.php
实例
<?php session_start(); if(!isset($_SESSION['user_id'])) { if ($_GET['m'] == 'login') { $conn = mysqli_connect('127.0.0.1', 'root', 'root', 'ttest') or die('failed'); $user = $_POST['user']; $pass = $_POST['pass']; $record = $_POST['record']; $query = "SELECT `user_id`,`userName` FROM `user` WHERE `userName`='{$user}' AND `userPass`='{$pass}'"; $res = mysqli_query($conn, $query); if (mysqli_num_rows($res) == 1) { if ($record == 1) { $row = mysqli_fetch_array($res); $_SESSION['user_id'] = $row['user_id']; $_SESSION['userName'] = $row['userName']; setcookie('user_id', $row['user_id'], time() + 3600); setcookie('userName', $row['userName'], time() + 3600); } echo json_encode([ 'status' => '0', 'reg_msg' => 'success' ]); } else { echo json_encode([ 'status' => '1', 'reg_msg' => 'error' ]); } } else { echo json_encode(['msg' => 'error!']); } }else{ header('Location:user.php'); }
运行实例 »
点击 "运行实例" 按钮查看在线实例
4.登出logout.php
实例
<?php session_start(); if(isset($_SESSION['user_id'])){ setcookie('user_id','',time()-3600); setcookie('userName','',time()-3600); $_SESSION = []; session_destroy(); setcookie('PHPSESSID','',time()-3600); } header('Location:login.html');
运行实例 »
点击 "运行实例" 按钮查看在线实例