php简易用户登录

| |
[不指定 2019/09/05 15:35 | by admin ]
1、登录login.php 用户admin密码123456

<?php
session_start();
if (isset($_POST['user'])) {
    $user = $_POST['user'];
    $password = $_POST['password'];
    if ($user == 'admin' && $password == '123456') {//验证正确
        $_SESSION['user'] = $user;
        //跳转到首页
        header('location:index.php');
    }else{
        echo "<script>alert('登录失败,用户名或密码不正确');</script>";
        exit();
    }
}
?>
<form method="POST">
    用户名: <input type="text" name="user"><br />
    密码: <input type="text" name="password"><br />
    <input type="submit" value="提交">
</form>


2、登录成功后index.php

<?php
session_start();
if (isset($_SESSION['user']) && !empty($_SESSION['user'])) {
    echo "登录成功:".$_SESSION['user'];
    echo "<a href='logout.php'>注销</a>";
}else{
    echo "你还没有登录,<a href='login.php'>请登录</a>";
}
?>

3、注销处理界面logout.php

<?php
    session_start();
    header('Content-type:text/html;charset=utf-8');
    if(isset($_SESSION['username']) && $_SESSION['username']==='admin'){
            session_unset();//free all session variable
            session_destroy();//销毁一个会话中的全部数据
            setcookie(session_name(),'',time()-3600);//销毁与客户端的卡号
            header('location:skip.php?url=index.php&info=注销成功,正在跳转!');
        }else{
            header('location:skip.php?url=index.php&info=注销失败,请稍后重试!');
        }
?>

4、跳转处理页面skip.php

<?php
    if(!isset($_GET['url']) || !isset($_GET['info'])){
        exit();
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="refresh" content="1"; url=login.php">
        <title>正在跳转中...</title>
    </head>
    <body>
        <div><?php echo $_GET['info'] ?></div>
    </body>
</html>

程序源码 | 评论(0) | 引用(0) | 阅读(187)