Blogger Information
Blog 39
fans 0
comment 0
visits 30863
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PDO第三课 : 貌似这节没有用到PDO,不过自己可以尝试一下;本节主要讲到了cookie和session的工作原理 2018年8月31日 22:38
南通税企通马主任的博客
Original
582 people have browsed it

1 , 编程: cookie实现用户登录与验证

实例

<?php
$page_title = '我是首页';
include 'mom/header.php';

echo '<h2 style="color: red">我是首页</h2>';

if (isset($_COOKIE['id']) && basename($_SERVER['PHP_SELF']) != 'logout.php'){
    echo '<a href="logout.php">退出</a>';
}else{
    echo '<a href="login.php">登录</a>';
}

include 'mom/footer.php';

运行实例 »

点击 "运行实例" 按钮查看在线实例



实例

<?php
$page_title = '用户登录';
include 'mom/header.php';
if (isset($errors) && !empty($errors)){
    $errors_msg = '<p style="color:red;">';
    foreach ($errors as $msg){
        $errors_msg .= $msg .'<br>';
    }echo $errors_msg .'</p>';
}

?>
    <h2 style="color: red;">用户登录</h2>
    <form action="login.php" method="post">
        <p>
            <lable for="email">邮箱</lable>
            <input type="email" name="email" id="email"
                   value="<?php echo isset($_POST['email']) ? $_POST['email'] :'' ?>">
        </p>
        <p>
            <lable for="password">密码</lable>
            <input type="password" name="password" id="password"
                   value="<?php echo isset($_POST['password']) ? $_POST['password'] :'' ?>">
        </p>
        <p>
            <button type="submit" name="submit" id="submit">登录</button>
        </p>
    </form>
<?php include 'mom/footer.php'?>


实例

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    require 'mom/function.php';

    require 'mom/mysql.php';

    list($check,$data) = check_login($dbs,$_POST['email'],$_POST['password']);

    if ($check){
//        开始设置cookie
        setcookie('id',$data['id']);
        setcookie('name',$data['name']);
//        设置跳转
        redirect_user('loggedin.php');
    }else{
        $errors = $data;
    }mysqli_close($dbs);
}

include 'login_page.php';


实例

<?php

if (!isset($_COOKIE['id'])){
    require 'mom/function.php';
    redirect_user();
}

$page_title = '登录成功';

include 'mom/header.php';

echo <<< "WELCOME"
<h2 style="color: red;">登录成功</h2>
<p>欢迎您:亲爱的{$_COOKIE['name']}</p>
<p><a href="logout.php">退出</a></p>
WELCOME;

include 'mom/footer.php';


实例

<?php

if (!isset($_COOKIE['id'])){
    require 'mom/function.php';
    redirect_user();
}else{
    setcookie('id','',time()-3600);
    setcookie('name','',time()-3600);

}

$page_title = '退出成功';

include 'mom/header.php';

echo <<< "WELCOME"
<h2 style="color: red;">退出成功</h2>

<p><a href="login.php">登录</a></p>
WELCOME;

include 'mom/footer.php';


实例

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>
        <?php
        echo isset($page_title) ? $page_title : '默认标题';
        ?>
    </title>
</head>
<body>
<h1>我是你们的头部</h1>


实例

<h1>我是你们的底部</h1>
</body>
</html>


实例

<?php

$dbs = mysqli_connect('127.0.0.1','root','root','php');

if (mysqli_connect_errno()){
    die('连接失败'.mysqli_connect_error());
}


实例

<?php

function check_login($dbs,$email='',$password){
    $errors = [];

    if (empty($email)){
        $errors[] = '邮箱不能为空';
    }else{
        $e = mysqli_escape_string($dbs,trim($email));
    }

    if (empty($password)){
        $errors[] = '密码不能为空';
    }else{
        $p = mysqli_escape_string($dbs,trim($password));
    }

    if (empty($errors)){
        $sql = "SELECT `id`,`name` FROM `user` WHERE `email`= '$e 'AND `password`= sha1('$p')";
//        die($sql);
        $res = mysqli_query($dbs,$sql);
//        var_dump($res);die;
//        echo mysqli_num_rows($res);die;
        if (mysqli_num_rows($res) == 1){
            $row = mysqli_fetch_array($res,MYSQLI_ASSOC);
//            print_r($row);die;
            return [true,$row];
        }else{
            $errors[] = '邮箱或密码不正确,请重新输入';
        }return [false,$errors];
    }
}

function redirect_user($page = 'index.php'){
    $url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
    $url = rtrim($url,'/\\');
    $url.= '/'.$page;
    header('location:'.$url);
    exit();
}



2 , 编程: session实现用户登录与验证

以下添加需要更改为session回话的页面 : 

实例

<?php
session_start();
$page_title = '我是首页';
include 'mom/header.php';

echo '<h2 style="color: red">我是首页</h2>';

if (isset($_SESSION['id']) && basename($_SERVER['PHP_SELF']) != 'logout.php'){
    echo '<a href="logout.php">退出</a>';
}else{
    echo '<a href="login.php">登录</a>';
}

include 'mom/footer.php';

实例

<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    require 'mom/function.php';

    require 'mom/mysql.php';

    list($check,$data) = check_login($dbs,$_POST['email'],$_POST['password']);

    if ($check){
//        开始设置cookie
//        setcookie('id',$data['id']);
//        setcookie('name',$data['name']);
//        设置session
        $_SESSION['id'] = $data['id'];
        $_SESSION['name'] = $data['name'];
//        设置跳转
        redirect_user('loggedin.php');
    }else{
        $errors = $data;
    }mysqli_close($dbs);
}

include 'login_page.php';

实例

<?php
session_start();
if (!isset($_SESSION['id'])){
    require 'mom/function.php';
    redirect_user();
}

$page_title = '登录成功';

include 'mom/header.php';

echo <<< "WELCOME"
<h2 style="color: red;">登录成功</h2>
<p>欢迎您:亲爱的{$_SESSION['name']}</p>
<p><a href="logout.php">退出</a></p>
WELCOME;

include 'mom/footer.php';

实例

<?php
session_start();
if (!isset($_SESSION['id'])){
    require 'mom/function.php';
    redirect_user();
}else{
//    setcookie('id','',time()-3600);
//    setcookie('name','',time()-3600);
session_destroy();
setcookie('PHPSESSION','',time()-3600);
}

$page_title = '退出成功';

include 'mom/header.php';

echo <<< "WELCOME"
<h2 style="color: red;">退出成功</h2>

<p><a href="login.php">登录</a></p>
WELCOME;

include 'mom/footer.php';

3 , 手写: cookie与session会话机制的优缺点分析

回答 : session会话机制相比较cookie会话机制而言更加安全 , 因为session是储存在服务器中 ;

所以除却安全性之外 , 还更加的易于操作和管理 !

 

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments