Blogger Information
Blog 22
fans 1
comment 1
visits 22658
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php之cookies和session
forever浅笑
Original
1029 people have browsed it

1.gif


login_page.php

实例

<?php
    $page_title = '用户登陆页';
?>
<?php include 'lib/header.php'?>
<h3>当前页: 登陆页</h3>


<div class="content">
    <form action="login.php" method="post">
        <p>
            <label for="email">邮箱</label>
            <input type="text" name="email" id="email">
        </p>
        <p>
            <label for="pass">密码</label>
            <input type="password" name="pass" id="pass">
        </p>
        <p><button type="submit">登陆</button></p>
    </form>
</div>

<?php include 'lib/footer.php'?>

运行实例 »

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

login.php

实例

<?php
// 判断是否post提交
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // 加载公共函数库
    require 'lib/function.php';

    // 连接数据库
    require 'lib/connect.php';

    // 验证登陆
    $email = $_POST['email'];
    $pass = $_POST['pass'];
    list($check,$data) = check_login($dbc,$email,$pass);

    // 验证通过
    if ($check) {
        setcookie('user_id',$data['user_id']);
        setcookie('user_name',$data['user_name']);
        redirect_user('../success.php');
    } else {
        $error = $data;
    }

    mysqli_close($dbc);

}

运行实例 »

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

\lib\connect.php

实例

<?php
$dbc = @mysqli_connect('localhost','root','root');
if (mysqli_connect_errno($dbc)) {
    echo '连接失败' . mysqli_connect_error();
} else {
//    echo '成功';
}
mysqli_select_db($dbc,'php');
mysqli_set_charset($dbc,'utf8');

运行实例 »

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

function.php

实例

<?php

/医院
 *用户自定义跳转
 * @param string $page
 */
function redirect_user($page = 'index.php')
{
    $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
    // 去掉右侧的 /,\
    $url = rtrim($url, '/\\');
    // 生成跳转地址
    $url .= '/' . $page;
    // 跳转到指定定址
    header('Location:' . $url);
    exit();
}

function check_login($dbc, $email, $pass)
{
    // 创建错误信息数组
    $errors = [];

    // 验证邮箱
    if (empty($email)) {
        $errors[] = '邮箱不能为空';
    } else {
        $e = mysqli_real_escape_string($dbc,trim($email));
    }

    // 验证密码
    if (empty($pass)) {
        $errors[] = '密码不能为空';
    } else {
        $p = mysqli_real_escape_string($dbc,trim($pass));
    }

    // 验证成功
    if (empty($errors)) {
        $sql = "select user_id,user_name from user where email = '$e' and  password = sha1('$p') ";
        $res = mysqli_query($dbc,$sql);
        if (mysqli_num_rows($res) == 1) {
            $row = mysqli_fetch_assoc($res);
            return [true,$row];
        } else {
            $errors[] = '邮箱或密码不正确,请重新输入';
        }
    }

    return [false,$errors];
}

运行实例 »

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

\lib\header.php

实例

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><?php echo isset($page_title) ? $page_title : '' ;?></title>
</head>
<body>
<h3>头部公共页</h3>

运行实例 »

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

\lib\footer.php

实例

<h3>底部公共页</h3>
</body>
</html>

运行实例 »

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

success.php

实例

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<?php
// 用户没有登陆跳转到登陆页
if (!isset($_COOKIE['user_id'])) {
    require 'lib/function.php';
    redirect_user('login_page.php');
}
?>
    登陆成功!!! 欢迎 <span style="color:red;"><?php echo !empty($_COOKIE['user_name']) ? $_COOKIE['user_name'] : '' ?></span> 的来到!     <a href="logout.php">注销</a>
</body>
</html>

运行实例 »

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

logout.php 

实例

<?php
// 用户没有登陆跳转到登陆页
if (!isset($_COOKIE['user_id'])) {
    require 'lib/function.php';
    redirect_user('login_page.php');
}
setcookie('user_id', '', time() - 3600);
setcookie('user_name', '', time() - 3600);
echo "<a href='login_page.php'>点击登陆</a>";
?>

运行实例 »

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


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
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!