入门PHP就来我这(高级)24 ~ Session判断用户登录

有胆量你就来跟着路老师卷起来! -- 纯干货,技术知识分享

路老师给大家分享PHP语言的知识了,旨在想让大家入门PHP,并深入了解PHP语言。


 上一篇我们介绍了Session管理部分的概念,本文通过session来改写一些用户登录,此时的用户名存储到session里。

 1 创建login2.php

<!DOCTYPE html>
<html lang="en" class="is-centered is-bold">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>登录页面</title>
  <link href="css/login.css" rel="stylesheet">
</head>
<body>
  <section style="background: transparent;">
    <form class="box py-3 px-4 px-2-mobile"  role="form"  method="post" action="checklogin2.php"  >
      <div class="is-flex is-column is-justified-to-center">
        <h1 class="title is-3 mb-a has-text-centered">
            登录
        </h1>
        <div class="inputs-wrap py-3">
          <div class="control">
            <input class="input" type="text" id="username" name="username" placeholder="用户名"   required></input>
          </div>
          <div class="control">
            <input class="input" type="password" id="password" name="password" placeholder="密码"   required></input>
          </div>
          <div class="control">
            <button class="button is-submit is-primary is-outlined" type="submit">
              提交
            </button>
          </div>
        </div>
      </div>
    </form>
  </section>
  
</body>
</html>

注意:上述代码种的样式表和Cookie免密登录的login.css相同。

 2 数据提交到checkLogin2.php

单击提交之后,表单会提交到checkLogin2.php文件,并在该文件中处理登录逻辑。登录成功后,使用session_start()函数初始化Session变量,将username存储到Session中。

checkLogin2.php代码如下:(请仔细查看和cookie文章内容的区别)

<?php 
  if(isset($_POST['username']) && isset($_POST['password'])){
    $username = trim($_POST['username']);
    $password = md5(trim($_POST['password']));
    require "config.php";
    try {
      $pdo = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER,DB_PWD);

    } catch (PDOException $e) {
      echo $e->getMessage();
    }

    $sql = 'select * from users where username = :username and password = :password';
    $result = $pdo->prepare($sql);
    $result->bindParam(':username',$username);
    $result->bindParam(':password',$password);
    if($result->execute()){
        $rows=$result->fetch(PDO::FETCH_ASSOC);
        if($rows){
          //启动Session
          session_start();
          $_SESSION['username']=$rows['username'];
          echo "<script>alert('恭喜您,登录成功!');
           window.location.href='index2.php';</script>";
        }else{
          echo "<script>alert('用户名或密码错误,登录失败!');
           history.back();</script>";
           exit();
        }
    }else{
      echo "<script>window.location.href='login2.php';</script>";
    }
  }
 
?>

3 获取Session中的数据

登录成功后,username存储到Session中,可以使用$_SESSION['username']获取该值,在index2.php文件中,实现代码如下:


<?php
date_default_timezone_set('PRC');
//开启Session
session_start();
//如果Session不存在,那就是第一次访问网站
if(!isset($_SESSION["username"])){
    echo "<script>alert('请先登录');
           window.location.href='login2.php';</script>";
}  
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>欢迎界面</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="container">
    <div class="jumbotron" style="background-color:#ff27d0">
       <h1>欢迎
          <span style="color:#363636;font-weight:700">
            <?php echo $_SESSION['username']; ?>

          </span>
          登录网站
       </h1>
       <p><a  class="btn btn-warning btn-lg" href="logout2.php" role="button">退出登录</a></p>
    </div>
</body>
</html>

4 退出登录logout2.php实现

<?php 
//启动Session
 session_start();
 //清除Session
 unset($_SESSION['username']);
 echo "<script>window.location.href='login2.php';</script>";
?>

 此文到此接触!

 下一篇 Session 高级应用


 

大家如果喜欢技术,并想有个好的交流平台可以关注我的 我的知乎首页,会不定期分享本人觉得比较好的技术类电子书。

另外,自己创建的一个技术qq群,玩转技术群,该群里功能:分享技能,电子书,源代码,以及兼职项目等交流,欢迎大家加入一起交流。

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 02:10:07       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 02:10:07       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 02:10:07       57 阅读
  4. Python语言-面向对象

    2024-07-12 02:10:07       68 阅读

热门阅读

  1. 算法·二分

    2024-07-12 02:10:07       17 阅读
  2. 解决AssertionError: Negative indexing is not supported

    2024-07-12 02:10:07       23 阅读
  3. PCL 点云PFH特征描述子

    2024-07-12 02:10:07       21 阅读
  4. 北京大学教育评论

    2024-07-12 02:10:07       24 阅读
  5. leetcode秋招冲刺 (专题16--18)

    2024-07-12 02:10:07       21 阅读
  6. 日常的网络杂记

    2024-07-12 02:10:07       19 阅读
  7. 设计模式之单例模式

    2024-07-12 02:10:07       21 阅读
  8. 软件架构之测评方法

    2024-07-12 02:10:07       15 阅读
  9. Webpack打包生产环境进行优化处理

    2024-07-12 02:10:07       20 阅读
  10. 【深度学习】关于模型加速

    2024-07-12 02:10:07       22 阅读
  11. k8s 部署RuoYi-Vue-Plus之mysql搭建

    2024-07-12 02:10:07       22 阅读
  12. 大数据面试题之Hudi(1)

    2024-07-12 02:10:07       18 阅读