Blogger Information
Blog 61
fans 1
comment 0
visits 69771
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0610-简易数据库调用登陆页面
我的博客
Original
784 people have browsed it

实例   login.php -表单登陆首页

<?php 
// 防止重复登录
if (isset($_COOKIE['username']) && $_COOKIE['username'] === 'admin') {
    echo '<script>alert("您已经登录,请不要重复登录");location.assign("admin.php");</script>';
    die;
}
?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
</head>
<body>
<h3>用户登录</h3>
<form action="check.php" method="post" onsubmit="return isEmpty()">
    <p>
        <label for="email">邮箱:</label>
        <input type="email" name="email" id="email">
    </p>

    <p>
        <label for="password">密码:</label>
        <input type="password" name="password" id="password">
    </p>

    <p>
        <button>提交</button>
    </p>
</form>

<script>
    // onsubmit: 表单提交前进行验证, 默认返回true
    // 非空验证函数  isEmpty 判断是否为空 ,isSet()判断是否有值
    function isEmpty() {
        var email = document.getElementById('email').value;
        var password = document.getElementById('password').value;
        if (email.length === 0 || password.length === 0 ) {
            alert('邮箱或密码不能为空');
            return false;
        }
    }

</script>

</body>
</html>

运行实例 »

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

实例

<?php  check.php- 判断用户数据是否正确

// 防止重复登录
if (isset($_COOKIE['username']) && $_COOKIE['username'] === 'admin') {
    echo '<script>alert("您已经登录,请不要重复登录");location.assign("admin.php");</script>';
    die;
}

//print_r($_POST);

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

$email = $_POST['email'];
$password = sha1($_POST['password']);

// 到用户表user中进行验证
$sql = 'SELECT * FROM `user` WHERE `email` = :email AND `password` = :password';

$stmt = $pdo->prepare($sql);
//把获取到的变量赋值给数据库查询变量进行查询(或者说互相绑定)
$stmt->execute([':email'=>$email,':password'=>$password]);


// fetch(): 成功返回数组,失败返回false
$user = $stmt->fetch(PDO::FETCH_ASSOC);


if ( $user===false) {
    // 登录失败
    echo '<script>alert("登陆失败,请检查");history.back();</script>';
    die;  // 终止当前脚本
}

// 登录成功,将用户信息写入cookie
setcookie('username', $user['username']);
setcookie('password', $user['password']);
echo '<script>alert("登录成功");location.assign("admin.php");</script>';

运行实例 »

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

实例  admin.php 后台页

<?php
// 禁止非授权用户访问
 // 连接数据库

require __DIR__ . '/inc/connect.php';


$sql = 'SELECT * FROM `user` WHERE `username`=:name';
$stmt =$pdo-> prepare($sql);
if(isset($_COOKIE['username'])){
$stmt->execute([':name'=>$_COOKIE['username']]);


$user = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
//echo '<pre>' . print_r($user,true);
if (isset($_COOKIE['username']) && $_COOKIE['username'] === $user[0]['username']):

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>后台首页</title>
</head>
<body>
<h1>后页首页</h1>
<p>欢迎:
<?php echo $_COOKIE['username']; ?>
</p>

<p><a href="logout.php">退出</a></p>
</body>
</html>

<?php else: ?>
    <script>alert("请登录");location.assign('login.php');</script>
    <?php endif; ?>

运行实例 »

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

实例  登出页面

<?php
// 必须在已登录的情况下,才允许退出
if (isset($_COOKIE['username']) && $_COOKIE['username'] === 'admin') {
    setcookie('username', null, time()-3600);
    echo '<script>alert("退出成功");location.href="login.php";</script>';
} else {
    echo '<script>alert("请先登录");location.assign("login.php");</script>';
}

运行实例 »

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


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