/
sessions.php
83 lines (71 loc) · 2.09 KB
/
sessions.php
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
session_start();
$name=$_POST['user'];
$pwd=$_POST['pwd'];
if (isset($_POST['on'])) {
if ($name && $pwd) {
$_SESSION['user']=$name;
$_SESSION['pwd']=$pwd;
if (isset($_SESSION['counter'])) {
$_SESSION['counter']+=1;
} else {
$_SESSION['counter']=1;
}
}
} elseif (isset($_POST['off'])) {
unset($_SESSION['user']);
unset($_SESSION['pwd']);
unset($_SESSION['counter']);
}
?>
<html>
<head><title>Access Control with Sessions</title></head>
<body>
<h1>Access Control with Sessions</h1>
<?php
if ($_POST['_check_']) {
checkForm();
} else {
printForm();
}
function checkForm() {
global $name, $pwd;
if (isset($_POST['show'])) {
echo "<h3>Session contains ".$_SESSION["user"]." and ".$_SESSION["pwd"]."</h3>";
echo "<h3>You have logged on ".$_SESSION["counter"] ." times</h3>";
} else {
if (!$name || !$pwd)
echo "<h3>Please enter username and password</h3>";
else
echo "<h3>You submitted $name and $pwd</h3>";
}
printForm();
} //checkForm
function printForm() {
$thisURL = $_SERVER['PHP_SELF'];
echo "<form action = " . $thisURL . " method='post'>";
} // printForm
?>
<table>
<tr>
<td>Enter your name</td>
<td><input type="text" length="20" name="user"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" length="10" name="pwd"></td>
</tr>
<tr>
<td><input type="submit" name="on" value="Log On"/></td>
<td><input type="submit" name="off" value="Log Off"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="show" value="Show the Session"/>
</td>
</tr>
<input type="hidden" name="_check_" value="1"/>
</table>
</form>
</body>
</html>