COOKIE知識點(diǎn)
1.設(shè)置: setcookie('username','admin');
2.讀取:echo $_COOKIE['username'];
3.更新: $_COOKIE['username']='peter zhu';
4.刪除:unset($_COOKIE['username']);
SESSION知識點(diǎn)
1.啟動會話 session_start();
2.設(shè)置:$_SESSION['username'] = 'peter zhu';
3.讀?。篹cho $_SESSION['username'];
4.刪除: session_unset(); 刪除session內(nèi)容
session_destory();刪除session文件;
刪除PHPSESSID setcookie('PHPSESSID','空',time()-3600);
COOKIE網(wǎng)站演示: pc.wenbus.cn/0726/cookie/index.php
SEESION網(wǎng)站演示: pc.wenbus.cn/0726/session/index.php
COOKIE演示案例代碼
index.php代碼
<?php if(isset($_COOKIE['username']) && $_COOKIE['username'] === 'admin'){ //已登錄顯示 echo '用戶:'.$_COOKIE['username'].'已登錄'; echo '<a href="dispatch.php?action=logout">退出</a>'; } else { //未登錄顯示 echo '<a href="dispatch.php?action=login">請登錄</a>'; }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
<?php // 請求派發(fā)器 require __DIR__ . '/inc/connect.php'; // 獲取請求參數(shù) $action = isset($_GET['action']) ? $_GET['action'] : 'login'; $action = htmlentities(strtolower(trim($action))); switch($action){ // 1.登錄頁面 case 'login': include __DIR__ . '/login.php'; break; //2.驗(yàn)證頁面 case 'check': include __DIR__ . '/check.php'; break; //3.退出頁面 case 'logout': include __DIR__ . '/logout.php'; break; //4.其他數(shù)據(jù) default: header('Location: index.php'); }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
<?php // 防止重復(fù)登錄 if(isset($_COOKIE['username']) && $_COOKIE['username'] === 'admin'){ echo '<script>alert("不要重復(fù)登錄");location.assign("index.php");</script>'; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用戶登錄</title> </head> <body> <form action="dispatch.php?action=check" method="POST" onsubmit="return isEmpty();"> <p> <label for="email">郵箱:</label> <input type="email" id="email" name="email"> </p> <p> <label for="password">密碼:</label> <input type="password" id="password" name="password"> </p> <p> <button>登錄</button> </p> </form> <script> // 防止不輸入數(shù)據(jù)提交 function isEmpty() { var email = document.getElementById('email').value; var password = document.getElementById('password').value; if (email.length === 0 || password.length === 0) { return false; } } </script> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
<?php // 先判斷是不是POST模式 if ($_SERVER['REQUEST_METHOD'] === 'POST'){ //獲取表單數(shù)據(jù) $email = $_POST['email']; $password = sha1($_POST['password']); // 輸入數(shù)據(jù)與數(shù)據(jù)表數(shù)據(jù)驗(yàn)證 $sql = 'SELECT * FROM `user` WHERE `email` =:email AND `password` =:password LIMIT 1'; $stmt = $pdo->prepare($sql); $stmt->execute(['email'=>$email,'password'=>$password]); $user = $stmt ->fetch(PDO::FETCH_ASSOC); if(false === $user){ echo '<script> alert("驗(yàn)證失敗");history.back();</script>'; die; } setcookie('username',$user['username']); echo '<script> alert("驗(yàn)證成功");location.assign("index.php")</script>'; exit; }else{ die("請求類型錯誤"); }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
<?php if (isset($_COOKIE['username']) && $_COOKIE['username']==='admin'){ setcookie('username',null,time()-3600); echo '<script>alert("退出成功");location.assign("login.php");</script>'; }else{ echo '<script>alert("請先登錄");location.assign("login.php")</script>'; }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
<?php session_start(); if (isset($_SESSION['username']) && $_SESSION['username']==='admin'){ echo '用戶:' .$_SESSION['username']. '已登錄<br>'; echo '<a href="dispatch.php?action=logout">注銷</a>'; }else{ echo '<a href="dispatch.php?action=login">請登錄</a>'; }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
<?php // session_start(); require __DIR__ .'/inc/connect.php'; $action = isset($_GET['action']) ? $_GET['action'] : 'login'; $action = htmlentities(strtolower(trim($action))); switch ($action){ case 'login': include __DIR__ .'/login.php'; break; case 'logout': include __DIR__ .'/logout.php'; break; case 'check': include __DIR__ .'/check.php'; break; default: header('Location:index.php'); }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
<?php session_start(); if ($_SERVER['REQUEST_METHOD']==='POST'){ $email = $_POST['email']; $password = sha1($_POST['password']); $sql = 'SELECT * FROM `user` WHERE `email` =:email AND `password` =:password LIMIT 1'; $stmt = $pdo->prepare($sql); $stmt -> execute(['email'=>$email,'password'=>$password]); $user = $stmt ->fetch(PDO::FETCH_ASSOC); // var_dump($user); if (false === $user){ echo '<script>alert("驗(yàn)證失敗");history.back()</script>'; die; } $_SESSION['username']= $user['username']; echo '<script>alert("驗(yàn)證成功");location.assign("index.php")</script>'; exit; }else{ die("請求類型錯誤"); }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
<?php session_start(); if (isset($_SESSION['username']) && $_SESSION['username']==='admin'){ echo '<script>alert("請不要重復(fù)登錄");location.assign("index.php");</script>'; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用戶登錄</title> </head> <body> <form action="dispatch.php?action=check" method="POST" onsubmit="return isEmpty();"> <p> <label for="email">郵箱:</label> <input type="email" id="email" name="email"> </p> <p> <label for="password">密碼:</label> <input type="password" id="password" name="password"> </p> <p> <button>登錄</button> </p> </form> <script> function isEmpty() { var email = document.getElementById('email').value; var password = document.getElementById('password').value; if(email.length===0||password.length===0){ return false; } } </script> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
<?php session_start(); if (isset($_SESSION['username']) && $_SESSION['username'] === 'admin') { session_unset(); setcookie('PHPSESSID', null, time()-3600); echo '<script>alert("退出成功");location.assign("index.php");</script>'; } else { echo '<script>alert("請先登錄");location.assign("login.php");</script>'; }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號