PHP会话控制实例代码与详细分析

本文实例讲述了PHP会话控制。分享给大家供大家参考,具体如下:

关于cookie和session的测试代码:

  1. <?php
  2. session_start();
  3. define('u','a');
  4. define('p','1');
  5. if(isset($_GET['r']) &&$_GET['r']== 1) {
  6. unset($_COOKIE['username']);
  7. unset($_COOKIE['password']);
  8. unset($_SESSION['valid_login']);
  9. }
  10. if(isset($_POST['username']) && isset($_POST['password'])) {
  11. $username=$_POST['username'];
  12. $password=$_POST['password'];
  13. if($username== u &&$password== p) {
  14. $_SESSION['valid_login'] = 1;
  15. setcookie('username',$username);
  16. setcookie('password',$password);
  17. }else{
  18. echo'incorrect u or p';
  19. }
  20. }
  21. ?>
  22. <?php
  23. if(isset($_SESSION['valid_login'])) {
  24. printf('welcom, %s',$_COOKIE['username']);
  25. }else{
  26. ?>
  27. <form action=""method="post">
  28. <input type="text"name="username"value=""/>
  29. <input type="password"name="password"value=""/>
  30. <input type="submit"/>
  31. </form>
  32. <?php
  33. }
  34. ?>
  35. <br />
  36. <a href="cookie_session.php"title="">refresh</a>
  37. <br />
  38. <a href="cookie_session.php?r=1"title="">clear</a>