PHP会话控制实例分析

时间:2022-03-04 09:43:47

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

关于cookie和session的测试代码:

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

希望本文所述对大家PHP程序设计有所帮助。