php基于cookie的简易登录模块

cookie 是用来将网站上的资料记录在客户端的技术,这种web技术让服务器将一些资料存放于客户端。比如当用户成功登录网站后,服务器会把用户信息保存到用户的cookie中,当再次访问同一个网站的其他脚本时就会携带cookie中的数据一起访问,在服务器的每一个脚本中都可以接受携带的cooike数据,不需要每次访问一个页面就重新输入一次登录者信息。

登录页面代码

<?php
header('Content-type:text/html;charset=utf-8');//设置编码格式防止乱码
if (isset($_POST['submit'])){
    
    //是否点击提交
    if (isset($_POST['username']) && isset($_POST['password']) && $_POST['username']==='ahua' && $_POST['password']==='12345'){
    
    
        //用户名和密码是否存在而且正确
        if (setcookie('username',$_POST['username'],time()+3600)){
    
    
            //将数据存入客户端
            header('Location:register.php');//跳转到成功页面
        }else{
    
    
            echo 'cokkie设置失败';
        }
    }else{
    
    
        echo '用户名或密码错误,登录失败';
    }
}
?>

<!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>Document</title>
</head>
<body>
<form method="post" action="login.php">//提交到当前页面
账号:<input type="text" name="username" ></br>
密码:<input type="password" name="password"></br>
<input type="submit" name="submit" value="登录">
</form>

</body>
</html>

登录成功页面代码

<?php
header('Content-type:text/html;charset=utf-8');//设置编码格式防止乱码
if (isset($_COOKIE['username']) && $_COOKIE['username']==='ahua') {
    
    
//cookie传入
    echo 'ahua欢迎回来';
}else{
    
    
    echo '<a href="login.php">请登录</a>';
}
?>

增加一个跳转页面和注销功能
跳转页面代码

<?php
if (!isset($_GET['url']) || !isset($_GET['info'])){
    
    
//地址栏没有url和提示内容时终止执行
    exit();
}
?>
<!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='refresh' content="3;URL=<?php echo $_GET['url']?>">
    //延迟三秒,跳转到传入的url地址
    <title>Document</title>
</head>
<body>
<div style="text-align: center"><?php echo$_GET['info']?></div>
//输出提示内容
</body>
</html>

增加后的登录页面

<?php
if (isset($_POST['submit'])){
    
    
    if (isset($_POST['username']) && isset($_POST['password']) && $_POST['username']==='ahua' && $_POST['password']==='12345'){
    
    
        if (setcookie('username','ahua',time()+3600)){
    
    
            header('Location:slip.php?url=register.php&info=登录成功,正在跳转中');
            //在浏览器地址栏中添加

        }else{
    
    
            echo 'cokkie设置失败';
        }
    }else{
    
    
        echo '用户名或密码错误,登录失败';
        header('Location:slip.php?url=register.php&info=对不起,用户名或密码错误,登录失败');

    }
}
?>

<!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>Document</title>
</head>
<body>
<br method="post" action="login.php">
    账号:<input type="text" name="username" ></br>
    密码:<input type="password" name="password"></br>
    <input type="submit" name="submit" value="登录">
</form>

</body>
</html>

注销页面

<?php
if (isset($_COOKIE['username']) && $_COOKIE['username']==='ahua' ){
    
    
    if (setcookie('username',$_POST['username'],time()-3600)){
    
    
    //使cookie失效
        header('Location:slip.php?url=register.php&info=正在注销中');
    }else{
    
    
        header('Location:slip.php?url=login.php&info=注销失败,请稍后重试');
    }
}

从左到右分别为:跳转页面名,登录成功页面名,登录页面名,注销页面名
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/aaahuahua/article/details/108010580