Home > Backend Development > PHP Tutorial > Example of php using cookie login authentication

Example of php using cookie login authentication

WBOY
Release: 2016-07-25 09:05:16
Original
874 people have browsed it
  1. Login
  2. 用户名:
    密码:
    Cookie保存时间:
复制代码

2、登录检测页 login.php

  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. $cookie = $_POST['cookie'];
  9. //执行SQL语句
  10. $query = @mysql_query("select username, userflag from users "
  11. ."where username = '$username' and passcode = '$passcode'")
  12. or die("SQL语句执行失败");
  13. //判断用户是否存在,密码是否正确
  14. if($row = mysql_fetch_array($query))
  15. {
  16. if($row['userflag'] == 1 or $row['userflag'] == 0) //判断用户权限信息是否有效
  17. {
  18. switch($cookie) //根据用户的选择设置cookie保存时间
  19. {
  20. case 0: //保存Cookie为浏览器进程
  21. setcookie("username", $row['username']);
  22. break;
  23. case 1: //保存1天
  24. setcookie("username", $row['username'], time()+24*60*60);
  25. break;
  26. case 2: //保存30天
  27. setcookie("username", $row['username'], time()+30*24*60*60);
  28. break;
  29. case 3: //保存365天
  30. setcookie("username", $row['username'], time()+365*24*60*60);
  31. break;
  32. }
  33. header("location: main.php"); //自动跳转到main.php
  34. }
  35. else
  36. {
  37. echo "用户权限信息不正确";
  38. }
  39. }
  40. else
  41. {
  42. echo "用户名或密码错误";
  43. }
  44. ?>
复制代码

3. Login successful verification page

  1. session_start();
  2. if(isset($_COOKIE['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 = $_COOKIE['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. //Obtain user permission information
  16. $flag = $row['userflag'];
  17. //Output different welcomes based on the value of userflag Information
  18. if($flag == 1)
  19. echo "Welcome administrator".$_SESSION['username']."Log in to the system";
  20. if($flag == 0)
  21. echo "Welcome user".$_SESSION[ 'username']."Log in to the system";
  22. echo "Logout";
  23. }
  24. else
  25. {
  26. echo "You You do not have permission to access this page";
  27. }
  28. ?>
Copy code

4. Log out and log in

  1. setcookie("username");
  2. echo "Logout successful";
  3. ?>
Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template