博客列表 >使用cookie、session 完成用户登录与退出-04.18

使用cookie、session 完成用户登录与退出-04.18

太早的博客
太早的博客原创
2019年04月22日 09:13:51888浏览

实例

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

运行实例 »

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


声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议