Home  >  Article  >  Backend Development  >  Example of session login verification in php

Example of session login verification in php

WBOY
WBOYOriginal
2016-07-25 09:05:17889browse
  1. Login
  2. 用户名:
    密码:
    Cookie保存时间:
复制代码

二、登录检测页

  1. @mysql_connect("localhost", "root","1981427") //选择数据库之前需要先连接数据库服务器
  2. or die("数据库服务器连接失败");
  3. @mysql_select_db("test") //选择数据库mydb
  4. or die("数据库不存在或不可用");
  5. //获取用户输入
  6. $username = $_POST['username'];
  7. $passcode = $_POST['passcode'];
  8. //执行SQL语句获得Session的值
  9. $query = @mysql_query("select username, userflag from users "
  10. ."where username = '$username' and passcode = '$passcode'")
  11. or die("SQL语句执行失败");
  12. //判断用户是否存在,密码是否正确
  13. if($row = mysql_fetch_array($query))
  14. {
  15. session_start(); //标志Session的开始
  16. //判断用户的权限信息是否有效,如果为1或0则说明有效
  17. if($row['userflag'] == 1 or $row['userflag'] == 0)
  18. {
  19. $_SESSION['username'] = $row['username'];
  20. $_SESSION['userflag'] = $row['userflag'];
  21. echo "欢迎登录,点击此处进入欢迎界面";
  22. }
  23. else //如果权限信息无效输出错误信息
  24. {
  25. echo "用户权限信息不正确";
  26. }
  27. }
  28. else //如果用户名和密码不正确,则输出错误
  29. {
  30. echo "用户名或密码错误";
  31. }
  32. ?>
复制代码

三、注销登录页

  1. unset($_SESSION['username']);
  2. unset($_SESSION['passcode']);
  3. unset($_SESSION['userflag']);
  4. echo "注销成功";
  5. ?>
复制代码

4. Successful login prompt page

  1. session_start();
  2. if(isset($_SESSION['username']))
  3. {
  4. @mysql_connect("localhost", "root","1981427") //Select You need to connect to the database server before the database
  5. or die("Database server connection failed");
  6. @mysql_select_db("test") //Select the database mydb
  7. or die("The database does not exist or is unavailable");
  8. //Get Session
  9. $username = $_SESSION['username'];
  10. //Execute the SQL statement to obtain the value of userflag
  11. $query = @mysql_query("select userflag from users "
  12. ."where username = '$username'")
  13. or die("SQL statement execution failed");
  14. $row = mysql_fetch_array($query);
  15. //Judge the permission information in the current database and compare it with the information in the Session. If they are different, update the Session information
  16. if($row[ 'userflag'] != $_SESSION['userflag'])
  17. {
  18. $_SESSION['userflag'] = $row['userflag'];
  19. }
  20. //Output different welcome messages according to the value of Session
  21. if( $_SESSION['userflag'] == 1)
  22. echo "Welcome administrator".$_SESSION['username']."Log in to the system";
  23. if($_SESSION['userflag'] == 0)
  24. echo "Welcome User ".$_SESSION['username']."Log in to the system";
  25. echo "Logout";
  26. }
  27. else
  28. {
  29. echo "You do not have permission to access this page";
  30. }
  31. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn