Blogger Information
Blog 18
fans 0
comment 0
visits 13465
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
使用cookie、session 完成用户登录与退出-04.18
太早的博客
Original
887 people have browsed it

实例

<?php
if(isset($_COOKIE['username']) && $_COOKIE['username'] === 'van'):
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>网站后台</title>
</head>
<body>
<h2>网站后台</h2>
<p>
    欢迎<?php echo $_COOKIE['username'];?>
</p>
<p>
    <a href="logout.php">退出</a>
</p>
</body>
</html>

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

运行实例 »

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

实例

<?php
if(isset($_COOKIE['username']) && $_COOKIE['username'] === 'van'){
    echo '<script>alert("已登录,不要重复登录");location.href="admin.php";</script>';
}
require  __DIR__ . '/include/pdo.php';

$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);
print_r($user);
if(false === $user){
    echo '<script>alert("登录失败,请检查");history.back();</script>';
    die;
}
setcookie('username',$user['username']);
echo '<script>alert("登录成功");location.assign("admin.php")</script>';

运行实例 »

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

实例

<?php
if(isset($_COOKIE['username']) && $_COOKIE['username'] === 'van'){
    echo '<script>alert("已登录,不要重复登录");location.href="admin.php";</script>';
}
require  __DIR__ . '/include/pdo.php';

$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);
print_r($user);
if(false === $user){
    echo '<script>alert("登录失败,请检查");history.back();</script>';
    die;
}
setcookie('username',$user['username']);
echo '<script>alert("登录成功");location.assign("admin.php")</script>';

运行实例 »

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

实例

<?php
if(isset($_COOKIE['username']) && $_COOKIE['username'] === 'van'){
    setcookie('username',null,time()-3600);
    echo '<script>alert("退出成功");location.href="login.php";</script>';
}else{
    echo '<script>alert("请先登录");location.href="login.php";</script>';
}

运行实例 »

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

实例

<?php
session_start();
if(isset($_SESSION['username']) && $_SESSION['username'] === 'van'):
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>网站后台</title>
</head>
<body>
<h2>网站后台</h2>
<p>
    欢迎<?php echo $_SESSION['username'];?>
</p>
<p>
    <a href="logout.php">退出</a>
</p>
</body>
</html>

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

运行实例 »

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

实例

<?php
session_start();
if(isset($_SESSION['username']) && $_SESSION['username'] === 'van'){
    echo '<script>alert("已登录,不要重复登录");location.href="admin.php";</script>';
}
require  __DIR__ . '/include/pdo.php';

$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);
print_r($user);
if(false === $user){
    echo '<script>alert("登录失败,请检查");history.back();</script>';
    die;
}
$_SESSION['username'] = $user['username'];
echo '<script>alert("登录成功");location.assign("admin.php")</script>';

运行实例 »

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

实例

<?php
session_start();
if(isset($_SESSION['username']) && $_SESSION['username'] === 'van'){
    echo '<script>alert("已登录,不要重复登录");location.href="admin.php";</script>';
}
?>
<!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>
    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
session_start();
if(isset($_SESSION['username']) && $_SESSION['username'] === 'van'){
    session_destroy();
    echo '<script>alert("退出成功");location.href="login.php";</script>';
}else{
    echo '<script>alert("请先登录");location.href="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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!