Blogger Information
Blog 27
fans 0
comment 0
visits 22355
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
cookie、session登陆
不乖的博客
Original
787 people have browsed it

实例

<?php
//判断用户是否已经登录
if( isset($_COOKIE['username']) && $_COOKIE['username'] === 'hms'){
    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
//防止重复登录的检查
if( isset($_COOKIE['username']) && $_COOKIE['username'] === 'hms'){
    echo '<script>alert("已经登录,请不要重复登录");location.href="admin.php";</script>';
}
//连接数据库
require  __DIR__.'/public/include/connect.php';

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

//到用户user表中进行验证
  $sql = 'SELECT * FROM `userInfo` WHERE `email` = :email AND `password` = :password LIMIT 1';
//  $sql = 'SELECT * FROM `userInfo` WHERE `email` = :email LIMIT 1';
//    创建语句对象,预处理对象
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':email',$email,PDO::PARAM_STR);
$stmt->bindParam(':password',$password,PDO::PARAM_STR);
$stmt->execute();
//fetch()成功返回数组,失败返回false
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if($user === false){
    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'] === 'hms'):
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>后台管理</title>
</head>
<body>
    <h3>后台管理</h3>
    <p>
        欢迎:<?php echo $_COOKIE['username'];?>
    </p>
    <p>
    <a href="logout.php">
        退出:<?php echo $_COOKIE['username'];?>
    </a>
    </p>
</body>
</html>
<?php else: ?>
    echo '<script>alert("请先登录");location.href="login.php";</script>';
<?php endif; ?>


----------------------------------------------------------------------------


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




session
----------------------------------------------------------------------------
<?php
session_start();
//判断用户是否已经登录
if( isset($_SESSION['username']) && $_SESSION['username'] === 'hms'){
    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'] === 'hms'){
    echo '<script>alert("已经登录,请不要重复登录");location.href="admin.php";</script>';
}
//连接数据库
require  __DIR__.'/public/include/connect.php';

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

//到用户user表中进行验证
  $sql = 'SELECT * FROM `userInfo` WHERE `email` = :email AND `password` = :password LIMIT 1';
//  $sql = 'SELECT * FROM `userInfo` WHERE `email` = :email LIMIT 1';
//    创建语句对象,预处理对象
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':email',$email,PDO::PARAM_STR);
$stmt->bindParam(':password',$password,PDO::PARAM_STR);
$stmt->execute();
//fetch()成功返回数组,失败返回false
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if($user === false){
    echo '<script>alert("登录失败,请检查");history.back();</script>';
    die; // 中断当前脚本
}
//登录成功
//setcookie('username',$user['username']);
$_SESSION['username'] = $user['username'];
echo '<script>alert("登录成功");location.assign("admin.php");</script>';



-----------------------------------------------------------------------------
<?php
session_start();
//判断用户是否已经登录
if( isset($_SESSION['username']) && $_SESSION['username'] === 'hms'):
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>后台管理</title>
</head>
<body>
    <h3>后台管理</h3>
    <p>
        欢迎:<?php echo $_SESSION['username'];?>
    </p>
    <p>
    <a href="logout.php">
        退出:<?php echo $_SESSION['username'];?>
    </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'] === 'hms'){
//    setcookie('username',null,time()-3600);
    session_destroy();
    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