亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 34
粉絲 1
評論 1
訪問量 47206
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
COOKIE登錄驗(yàn)證與SESSION登錄驗(yàn)證小案例——2019年7月26日22時08分
嘿哈的博客
原創(chuàng)
824人瀏覽過

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>';
    }

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

請求派發(fā)器dispatch.php實(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');
    }

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

LOGIN.PHP登錄表單實(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>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

CHECK.PHP驗(yàn)證代碼實(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("請求類型錯誤");
    }

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

LOGOUT.PHP退出登錄代碼實(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>';
}

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

session演示案例代碼

index.php主頁實(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>';
    }

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例


PHP請求分發(fā)器dispatch.php實(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');
    }

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

驗(yàn)證check.php實(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("請求類型錯誤");
    }

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

登錄頁面login.php實(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>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

退出logout.php實(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>';
}

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例



批改狀態(tài):合格

老師批語:用unset()刪除COOKIE時要注意, 千萬不要把$_COOKIE刪除了
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)