cookie實(shí)現(xiàn)登錄驗(yàn)證、退出和注冊(cè)功能:
1-1 登錄頁面
<?php if (isset($_COOKIE['username'])) { echo '<script>alert("賬號(hào)已登錄");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">賬號(hào):</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('賬號(hào)或密碼不能為空'); return false; } } </script> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
1-2 控制臺(tái)頁面
<?php require __DIR__.'\public-files/connect.php'; $action = isset($_GET['action'])?$_GET['action']:'login'; $actoin = htmlentities(strtolower(trim($action)));//HTML符號(hào)轉(zhuǎn)實(shí)體,轉(zhuǎn)換小寫,去除左右空格 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; }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
1-3 登錄驗(yàn)證后臺(tái)代碼
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { //獲取表單參數(shù) $username = $_POST['username']; $password = sha1($_POST['password']); //連接數(shù)據(jù)庫進(jìn)行驗(yàn)證 $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); //驗(yàn)證失敗 if (false === $user) { echo "<script>alert('驗(yàn)證失敗');history.back()</script>"; die; } //驗(yàn)證成功 setcookie('username',$username); echo "<script>alert('登錄成功');location.assign('index.php');</script>"; }else{ die('請(qǐng)求類型錯(cuò)誤'); }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
1-4 首頁顯示用戶名
<?php //引用PDO實(shí)例化 include 'connect.php'; //首頁數(shù)據(jù) $sql = "SELECT * FROM `system` WHERE `sys_id`=1"; $mimt = $pdo->prepare($sql); $mimt->execute(); $system = $mimt->fetch(PDO::FETCH_ASSOC);//查詢單條數(shù)據(jù) //列表數(shù)據(jù) $sql = "SELECT * FROM `cates`"; $mimt = $pdo->prepare($sql); $mimt->execute(); $cates = $mimt->fetchAll(PDO::FETCH_ASSOC); //內(nèi)容數(shù)據(jù) $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">注冊(cè)</a>'; echo '<a href="dispatch.php?action=login" class="dl">登錄</a>'; } ?> </ul> </div> <div class="clear"></div>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
1-5 退出登錄后臺(tái)代碼
<?php if (isset($_COOKIE['username'])) { setcookie('username',null,time()-3600); echo '<script>alert("退出成功");location.assign("index.php");</script>'; }else{ // 要求用戶先登錄 echo '<script>alert("請(qǐng)先登錄");location.assign("denglu.php");</script>'; }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
1-6 注冊(cè)頁面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注冊(cè)</title> <link rel="stylesheet" href="static/css/denglu.css"> </head> <body> <div class="center"> <div class="jz"> <h2>注冊(cè):</h2> <form action="dispatch.php?action=zcyz" method="post" onsubmit="return zcyz()"> <p> <label for="username">賬號(hào)?。?lt;/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">注冊(cè)</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('賬號(hào)或密碼不能為空'); return false; } if (password1 != password2) { alert('兩次密碼輸入不一致'); return false; } } </script> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
1-7 注冊(cè)后臺(tái)驗(yàn)證代碼
<?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) { //連接數(shù)據(jù)庫進(jìn)行驗(yàn)證 $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('此賬號(hào)已注冊(cè)');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("注冊(cè)成功");location.assign("index.php");</script>'; }else{ echo '<script>alert("注冊(cè)失敗");history.back();</script>'; } }else{ echo '<script>alert("兩次密碼輸入不一致");history.back();</script>'; } }else{ die('請(qǐng)求類型錯(cuò)誤'); }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
session注冊(cè)功能:
session與cookie最重要的區(qū)別是要開啟session_start會(huì)話,
以下只簡單展示注冊(cè)功能的session流程
2-1注冊(cè)頁面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注冊(cè)</title> <link rel="stylesheet" href="static/css/denglu.css"> </head> <body> <div class="center"> <div class="jz"> <h2>注冊(cè):</h2> <form action="dispatch.php?action=zcyz" method="post" onsubmit="return zcyz()"> <p> <label for="username">賬號(hào)?。?lt;/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">注冊(cè)</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('賬號(hào)或密碼不能為空'); return false; } if (password1 != password2) { alert('兩次密碼輸入不一致'); return false; } } </script> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
2-2 控制臺(tái)(此文檔開啟session會(huì)話)
<?php session_start(); require __DIR__.'\public-files/connect.php'; $action = isset($_GET['action'])?$_GET['action']:'login'; $actoin = htmlentities(strtolower(trim($action)));//HTML符號(hào)轉(zhuǎn)實(shí)體,轉(zhuǎn)換小寫,去除左右空格 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; }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
2-3 注冊(cè)驗(yàn)證代碼
<?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) { //連接數(shù)據(jù)庫進(jìn)行驗(yàn)證 $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('此賬號(hào)已注冊(cè)');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("注冊(cè)成功");location.assign("index.php");</script>'; }else{ echo '<script>alert("注冊(cè)失敗");history.back();</script>'; } }else{ echo '<script>alert("兩次密碼輸入不一致");history.back();</script>'; } }else{ die('請(qǐng)求類型錯(cuò)誤'); }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
2-4 首頁顯示用戶名
<?php session_start(); //引用PDO實(shí)例化 include 'connect.php'; //首頁數(shù)據(jù) $sql = "SELECT * FROM `system` WHERE `sys_id`=1"; $mimt = $pdo->prepare($sql); $mimt->execute(); $system = $mimt->fetch(PDO::FETCH_ASSOC);//查詢單條數(shù)據(jù) //列表數(shù)據(jù) $sql = "SELECT * FROM `cates`"; $mimt = $pdo->prepare($sql); $mimt->execute(); $cates = $mimt->fetchAll(PDO::FETCH_ASSOC); //內(nèi)容數(shù)據(jù) $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">注冊(cè)</a>'; echo '<a href="dispatch.php?action=login" class="dl">登錄</a>'; } ?> </ul> </div> <div class="clear"></div>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)