cookie实现登录验证、退出和注册功能:
1-1 登录页面
实例
<?php if (isset($_COOKIE['username'])) { echo '<script>alert("账号已登录");location.assign("index.php");</script>'; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录</title> <link rel="stylesheet" href="static/css/denglu.css"> </head> <body> <div class="center"> <div class="jz"> <h2>登录:</h2> <form action="dispatch.php?action=check" method="post" onsubmit="return dlyz();"> <p> <label for="username">账号:</label> <input type="text" name="username" id="username"> </p> <p> <label for="password">密码:</label> <input type="password" name="password" id="password"> </p> <p style="margin-left: 100px;"> <button type="submit">登录</button> </p> </form> </div> </div> <script> function dlyz(){ var username = document.getElementById('username').value; var password = document.getElementById('password').value; if (username.length === 0 || password.length === 0) { alert('账号或密码不能为空'); return false; } } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
1-2 控制台页面
实例
<?php require __DIR__.'\public-files/connect.php'; $action = isset($_GET['action'])?$_GET['action']:'login'; $actoin = htmlentities(strtolower(trim($action)));//HTML符号转实体,转换小写,去除左右空格 switch ($action) { case 'login': include __DIR__.'\denglu.php'; break; case 'check': include __DIR__.'\check.php'; break; case 'tuichu': include __DIR__.'\tuichu.php'; break; case 'zcyz': include __DIR__.'\zcyz.php'; break; case 'zhuce': include __DIR__.'\zhuce.php'; break; default: header('location:index.php'); break; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
1-3 登录验证后台代码
实例
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { //获取表单参数 $username = $_POST['username']; $password = sha1($_POST['password']); //连接数据库进行验证 $sql = 'SELECT * FROM `user` WHERE `username`= :username AND `password`= :password LIMIT 1'; $stmt = $pdo->prepare($sql); // $stmt->bindparam(':username',$username,PDO::PARAM_STR); // $stmt->bindparam(':password',$password,PDO::PARAM_STR); $stmt->execute(['username'=>$username,'password'=>$password]); $user= $stmt->fetch(PDO::FETCH_ASSOC); //验证失败 if (false === $user) { echo "<script>alert('验证失败');history.back()</script>"; die; } //验证成功 setcookie('username',$username); echo "<script>alert('登录成功');location.assign('index.php');</script>"; }else{ die('请求类型错误'); }
运行实例 »
点击 "运行实例" 按钮查看在线实例
1-4 首页显示用户名
实例
<?php //引用PDO实例化 include 'connect.php'; //首页数据 $sql = "SELECT * FROM `system` WHERE `sys_id`=1"; $mimt = $pdo->prepare($sql); $mimt->execute(); $system = $mimt->fetch(PDO::FETCH_ASSOC);//查询单条数据 //列表数据 $sql = "SELECT * FROM `cates`"; $mimt = $pdo->prepare($sql); $mimt->execute(); $cates = $mimt->fetchAll(PDO::FETCH_ASSOC); //内容数据 $sql = "SELECT * FROM `movies`"; $mimt = $pdo->prepare($sql); $mimt->execute(); $movies = $mimt->fetchAll(PDO::FETCH_ASSOC); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="keywords" content='<?php echo $system["key"] ?>'> <meta name="fdsjkflk" content='<?php echo $system["desc"] ?>'> <link rel="stylesheet" href="static/css/index.css"> <link rel="shortcut icon" type="image/x-icon" href="static/images/<?php echo $system['logo'] ?>"> <title><?php echo $system['title'] ?></title> </head> <body> <!-- 头部 --> <div class="header"> <ul> <div style="float: left;margin-left: 50px;"><a href="index.php"><img src="static/images/<?php echo $system['logo'] ?>" alt="" width="50" height="30"></a></div> <li style="border-left: none;"><a href="./index.php">首页</a></li> <?php foreach($cates as $cate): ?> <li><a href="./list.php?cate_id=<?php echo $cate['cate_id']?>"><?php echo $cate['alias'] ?></a></li> <?php endforeach; ?> <li><a href="http://zhibo.renren***/">直播</a></li> <?php if (isset($_COOKIE['username'])) { echo '<a href="dispatch.php?action=tuichu" class="dl">退出</a>'; echo'<a class="dl">'.$_COOKIE["username"].'</a>'; } else{ echo '<a href="dispatch.php?action=zhuce" class="dl">注册</a>'; echo '<a href="dispatch.php?action=login" class="dl">登录</a>'; } ?> </ul> </div> <div class="clear"></div>
运行实例 »
点击 "运行实例" 按钮查看在线实例
1-5 退出登录后台代码
实例
<?php if (isset($_COOKIE['username'])) { setcookie('username',null,time()-3600); echo '<script>alert("退出成功");location.assign("index.php");</script>'; }else{ // 要求用户先登录 echo '<script>alert("请先登录");location.assign("denglu.php");</script>'; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
1-6 注册页面
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注册</title> <link rel="stylesheet" href="static/css/denglu.css"> </head> <body> <div class="center"> <div class="jz"> <h2>注册:</h2> <form action="dispatch.php?action=zcyz" method="post" onsubmit="return zcyz()"> <p> <label for="username">账号 :</label> <input type="text" name="username" id="username"> </p> <p> <label for="password1">密码1:</label> <input type="password" name="password1" id="password1"> </p> <p> <label for="password2">密码2:</label> <input type="password" name="password2" id="password2"> </p> <p style="margin-left: 100px;"> <button type="submit">注册</button> </p> </form> </div> </div> <script> function zcyz(){ var username = document.getElementById('username').value; var password1 = document.getElementById('password1').value; var password2 = document.getElementById('password2').value; if (username.length === 0 || password1.length === 0 || password2.length === 0) { alert('账号或密码不能为空'); return false; } if (password1 != password2) { alert('两次密码输入不一致'); return false; } } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
1-7 注册后台验证代码
实例
<?php if ($_SERVER['REQUEST_METHOD'] ==='POST') { $username = htmlentities(strtolower(trim($_POST['username']))); $password1 = sha1(htmlentities(strtolower(trim($_POST['password1'])))); $password2 = sha1(htmlentities(strtolower(trim($_POST['password2'])))); if ($password1 === $password2) { //连接数据库进行验证 $sql = 'SELECT * FROM `user` WHERE `username`= :username LIMIT 1'; $stmt = $pdo->prepare($sql); // $stmt->bindparam(':username',$username,PDO::PARAM_STR); // $stmt->bindparam(':password',$password,PDO::PARAM_STR); $stmt->execute(['username'=>$username]); $user= $stmt->fetch(PDO::FETCH_ASSOC); if ($username === $user['username']) { echo "<script>alert('此账号已注册');history.back()</script>"; die; } $sql = 'INSERT INTO `user` SET `username`=:username,`password`=:password'; $stmt = $pdo->prepare($sql); $stmt->execute(['username'=>$username,'password'=>$password2]); if ($stmt->rowCount()>0) { setcookie('username',$username); echo '<script>alert("注册成功");location.assign("index.php");</script>'; }else{ echo '<script>alert("注册失败");history.back();</script>'; } }else{ echo '<script>alert("两次密码输入不一致");history.back();</script>'; } }else{ die('请求类型错误'); }
运行实例 »
点击 "运行实例" 按钮查看在线实例
session注册功能:
session与cookie最重要的区别是要开启session_start会话,
以下只简单展示注册功能的session流程
2-1注册页面
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注册</title> <link rel="stylesheet" href="static/css/denglu.css"> </head> <body> <div class="center"> <div class="jz"> <h2>注册:</h2> <form action="dispatch.php?action=zcyz" method="post" onsubmit="return zcyz()"> <p> <label for="username">账号 :</label> <input type="text" name="username" id="username"> </p> <p> <label for="password1">密码1:</label> <input type="password" name="password1" id="password1"> </p> <p> <label for="password2">密码2:</label> <input type="password" name="password2" id="password2"> </p> <p style="margin-left: 100px;"> <button type="submit">注册</button> </p> </form> </div> </div> <script> function zcyz(){ var username = document.getElementById('username').value; var password1 = document.getElementById('password1').value; var password2 = document.getElementById('password2').value; if (username.length === 0 || password1.length === 0 || password2.length === 0) { alert('账号或密码不能为空'); return false; } if (password1 != password2) { alert('两次密码输入不一致'); return false; } } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
2-2 控制台(此文档开启session会话)
实例
<?php session_start(); require __DIR__.'\public-files/connect.php'; $action = isset($_GET['action'])?$_GET['action']:'login'; $actoin = htmlentities(strtolower(trim($action)));//HTML符号转实体,转换小写,去除左右空格 switch ($action) { case 'login': include __DIR__.'\denglu.php'; break; case 'check': include __DIR__.'\check.php'; break; case 'tuichu': include __DIR__.'\tuichu.php'; break; case 'zcyz': include __DIR__.'\zcyz.php'; break; case 'zhuce': include __DIR__.'\zhuce.php'; break; default: header('location:index.php'); break; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
2-3 注册验证代码
实例
<?php if ($_SERVER['REQUEST_METHOD'] ==='POST') { $username = htmlentities(strtolower(trim($_POST['username']))); $password1 = sha1(htmlentities(strtolower(trim($_POST['password1'])))); $password2 = sha1(htmlentities(strtolower(trim($_POST['password2'])))); if ($password1 === $password2) { //连接数据库进行验证 $sql = 'SELECT * FROM `user` WHERE `username`= :username LIMIT 1'; $stmt = $pdo->prepare($sql); // $stmt->bindparam(':username',$username,PDO::PARAM_STR); // $stmt->bindparam(':password',$password,PDO::PARAM_STR); $stmt->execute(['username'=>$username]); $user= $stmt->fetch(PDO::FETCH_ASSOC); if ($username === $user['username']) { echo "<script>alert('此账号已注册');history.back()</script>"; die; } $sql = 'INSERT INTO `user` SET `username`=:username,`password`=:password'; $stmt = $pdo->prepare($sql); $stmt->execute(['username'=>$username,'password'=>$password2]); if ($stmt->rowCount()>0) { $_SESSION['username'] = $username; echo '<script>alert("注册成功");location.assign("index.php");</script>'; }else{ echo '<script>alert("注册失败");history.back();</script>'; } }else{ echo '<script>alert("两次密码输入不一致");history.back();</script>'; } }else{ die('请求类型错误'); }
运行实例 »
点击 "运行实例" 按钮查看在线实例
2-4 首页显示用户名
实例
<?php session_start(); //引用PDO实例化 include 'connect.php'; //首页数据 $sql = "SELECT * FROM `system` WHERE `sys_id`=1"; $mimt = $pdo->prepare($sql); $mimt->execute(); $system = $mimt->fetch(PDO::FETCH_ASSOC);//查询单条数据 //列表数据 $sql = "SELECT * FROM `cates`"; $mimt = $pdo->prepare($sql); $mimt->execute(); $cates = $mimt->fetchAll(PDO::FETCH_ASSOC); //内容数据 $sql = "SELECT * FROM `movies`"; $mimt = $pdo->prepare($sql); $mimt->execute(); $movies = $mimt->fetchAll(PDO::FETCH_ASSOC); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="keywords" content='<?php echo $system["key"] ?>'> <meta name="fdsjkflk" content='<?php echo $system["desc"] ?>'> <link rel="stylesheet" href="static/css/index.css"> <link rel="shortcut icon" type="image/x-icon" href="static/images/<?php echo $system['logo'] ?>"> <title><?php echo $system['title'] ?></title> </head> <body> <!-- 头部 --> <div class="header"> <ul> <div style="float: left;margin-left: 50px;"><a href="index.php"><img src="static/images/<?php echo $system['logo'] ?>" alt="" width="50" height="30"></a></div> <li style="border-left: none;"><a href="./index.php">首页</a></li> <?php foreach($cates as $cate): ?> <li><a href="./list.php?cate_id=<?php echo $cate['cate_id']?>"><?php echo $cate['alias'] ?></a></li> <?php endforeach; ?> <li><a href="http://zhibo.renren***/">直播</a></li> <?php if (isset($_SESSION['username'])) { echo '<a href="dispatch.php?action=tuichu" class="dl">退出</a>'; echo'<a class="dl">'.$_SESSION["username"].'</a>'; } else{ echo '<a href="dispatch.php?action=zhuce" class="dl">注册</a>'; echo '<a href="dispatch.php?action=login" class="dl">登录</a>'; } ?> </ul> </div> <div class="clear"></div>
运行实例 »
点击 "运行实例" 按钮查看在线实例