当前位置:首页 > PHP教程 > php会话 > 列表

php中利用session验证登录表单

发布:smiling 来源: PHP粉丝网  添加日期:2016-01-01 14:56:15 浏览: 评论:0 

前段时间在做一个中奖的活了,其中就用到中奖之后把数据写入session 然后再由用户进行数据提交验证了,下面我们要介绍的不是那个例子而一个差不多例子了,具体如下。

登录页面是:

  1. <!doctype html> 
  2. <html lang="en"> 
  3. <head> 
  4. <meta charset="UTF-8"> 
  5. <title>登陆</title> 
  6. </head> 
  7. <body> 
  8. <form name="login" action="login.php" method="post"> 
  9. 姓名:<input type=text name="name"><br/> 
  10. 密码:<input type=password name="password"><br/> 
  11. <!-- <input type="radio" name="limits" value="1">管理员 --> 
  12. <!-- <input type="radio" name="limits" value="0">普通用户 --> 
  13. <input type="submit" name="submit" value="登录"> 
  14. </form> 
  15. </body> 
  16. </html> 

存储session的页面:

  1. <?php 
  2. header("Content-Type: text/html; charset=utf8"); 
  3. if( !isset($_POST["submit"]) ){ 
  4. die("错误执行"); 
  5. }//检测是否有submit操作 
  6.  
  7. require_once('connect.php');//链接数据库 
  8.  
  9. if ( isset($_POST['name']) && isset($_POST['password']) ){//如果用户名和密码都不为空 
  10.  
  11. $name = $_POST['name']; 
  12.  
  13. $password = $_POST['password']; 
  14.  
  15. $sql = " SELECT id, limits, message FROM user WHERE username = '$name' AND password = '$password' LIMIT 1"
  16.  
  17. $result = mysqli_query( $con , $sql );//执行sql 用户名和密码 
  18.  
  19. $rows = mysqli_num_rows( $result );//返回用户名密码是否存在 
  20.  
  21. if$rows != 0 ){ 
  22.  
  23. session_start(); 
  24.  
  25. while$rows_other = mysqli_fetch_assoc($result) ){ 
  26.  
  27. $_SESSION['id'] = $rows_other['id']; 
  28. $_SESSION['name'] = $name
  29. $_SESSION['limits'] = $rows_other['limits']; 
  30. $_SESSION['message'] = $rows_other['message']; 
  31.  
  32.  
  33. header("refresh:0;url=welcome.php");//跳转至welcome.html页面 
  34.  
  35. exit
  36.  
  37. }else
  38.  
  39. echo "用户名或密码错误"
  40.  
  41. echo "<script> 
  42. alert('用户名或密码错误'); 
  43. setTimeout(function(){window.location.href='login.html';},1000); 
  44. </script>"; 
  45.  
  46.  
  47. }else
  48.  
  49. echo "表单填写不完整"
  50. //phpfensi.com 
  51. echo "<script> 
  52. alert('表单填写不完整'); 
  53. setTimeout(function(){window.location.href='login.html';},1000); 
  54. </script>"; 
  55.  
  56. ?> 

登陆后跳转的页面,根据不同的用户显示不同的权限和用户名:

  1. <?php 
  2.  
  3. <!DOCTYPE html> 
  4. <html> 
  5. <head> 
  6. <meta charset="UTF-8"
  7. <title>Document</title> 
  8. </head> 
  9. <body> 
  10.  
  11. <?php 
  12.  
  13. session_start(); 
  14.  
  15. if( isset($_SESSION['id']) ){ 
  16.  
  17. require_once('connect.php'); 
  18.  
  19. $id = $_SESSION['id']; 
  20. $name = $_SESSION['name']; 
  21. $limits = $_SESSION['limits']; 
  22. $message = $_SESSION['message']; 
  23.  
  24. if$limits == 1 ){ 
  25.  
  26. echo 'hello, 管理员' . '<br/>'
  27.  
  28. }else
  29.  
  30. echo 'helo, 普通用户' . '<br/>'
  31.  
  32. echo 'hello you name is:' . $name
  33.  
  34. }else
  35.  
  36. echo '未登录!'
  37.  
  38. header("refresh:3;url=login.html"); 
  39.  
  40. ?> 
  41.  
  42. &nbsp; 
  43. </body> 
  44. </html> 
  45. ?> 

使用session注意事项

1.在当前页面要使用session时我们在文件最前面没有输入内容时加上session_start();

2.session有一个时间限制的这个我们可以进行修改的,具体如下

其实PHP5 Session还提供了一个函数 session_set_cookie_params(); 来设置PHP5 Session的生存期的,该函数必须在 session_start() 函数调用之前调用:

  1. <?php 
  2.     // 保存一天 
  3.     $lifeTime = 24 * 3600; 
  4.     session_set_cookie_params($lifeTime); 
  5.     session_start(); 
  6. ?> 

Tags: session验证表单 php验证表单

分享到: