Sunteți pe pagina 1din 12

50

$userVerified = 0;

51
52

// read each line in file


and check
username
Before
entering
the while

53

// and password

54

while ( !feof(

loop, variable
Function$userVerified
fgets reads a line
from
is set
to 0the
. text file.
$file
) && !$userVerified
) {
The result
is assigned to variable
$line.

55
56
57
58
59
60
61
62
63

password.php
(3 of 7)

// read line from file

The while loop executes as long


as the
are more
removes
thethere
newline
character
lines in the filefrom
to read
and of
variable
$userVerified is
the end
the line.
Function
split is called to separate the string at the
still
0 or empty.
// remove newline character from end of line
specified delimiter (in this case, a comma). The
$line = chop( $line );
The username
by the user
resulting array
is stored inentered
array $field
. is tested
against the one returned in the text file (stored
// split username and password
in the first element of the array). If they match,
$field = split( ",", $line, 2 );
variable $userVerified is set to 1.
chop
$line = fgets( Function
$file, 255
);

64
65

// verify username

66

if ( $USERNAME == $field[ 0 ] ) {

67

$userVerified = 1;

68

checkPassword returns true, function


accessGranted is called to notify the client that

69

function
// call function checkPassword to If
verify

70

// users password

71

permission
if ( checkPassword( $PASSWORD, $field
)

has been granted. Otherwise, function


wrongPassword is called.

72

== true )

73

accessGranted( $USERNAME );

74
75

else
wrongPassword();

Function checkPassword is called to verify the


users password. Variable $PASSWORD and array
$field are passed to the function.

76
77

78

81

If variable $userVerified has not been set to a


while loop
has executed,
value
otherAfter
than 0the
, function
accessDenied
is function
// close text fi
le
fclose
is called
close the
called to notify
the client
thattoaccess
has file.
been
password.php
fclose( $file );
denied.
(4 of 7)

82

// call function accessDenied if username has

83

// not been verified

84

if ( !$userVerified )

85

accessDenied();

79
80

86

87
88

Function checkPassword compares the users


password to the password in the file. If they match,
true is returned, whereas false is returned if they
do not.

// verify user password and return a boolean

89

function checkPassword( $userpassword, $filedata )

90

{
if ( $userpassword == $filedata[ 1 ] )

91

return true;

92

else

93

return false;

94
95

96

97

// print a message indicating the user has been added

98

function userAdded( $name )

99

{
print( "<title>Thank You</title></head>

100
101

<body style = \"font-family:


arial;
Function userAdded

102

font-size: 1em;

103

<strong>You have been added

104

to the user list, $name.

105

<br />Enjoy the

106

prints a message to the


color:
blue\">
client
indicating that the user has been added.

password.php
(5 of 7)

Function accessGranted prints a


site.</strong>"message
);
to the client indicating that
permission has been granted.

107
108

// print a message indicating permission

109

// has been granted

110

function accessGranted( $name )

111

{
print( "<title>Thank You</title></head>

112
113

<body style = \"font-family: arial;

114

font-size: 1em; color: blue\">

115

<strong>Permission has been

116

granted, $name. <br />

117

Enjoy the site.</strong>" );

118

119

120

// print a message indicating password is invalid

121

function wrongPassword()

122

125

Function wrongPassword prints a message to the


client indicating that the password is invalid.
password.php
<body style = \"font-family: arial;
(6 of 7)
font-size: 1em; color: red\">

126

<strong>You entered an invalid

127

password.<br />Access has

128

been denied.</strong>" );

print( "<title>Access Denied</title></head>

123
124

129

130
131

// print a message indicating access has been denied

132

function accessDenied()

133

{
print( "<title>Access Denied</title></head>

134
135

<body style = \"font-family: arial;

136

font-size: 1em; color: red\">

137

<strong>

138

You were denied access to this server.

139

<br /></strong>" );

140

Function accessDenied prints a message to the


client indicating that access has been denied.

141

1
4
2

/
/p
r
i
n
t am
e
s
s
a
g
ei
n
d
i
c
a
t
i
n
gt
h
a
tf
i
e
l
d
s

1
4
3

/
/h
a
v
eb
e
e
nl
e
f
tb
l
a
n
k

Function fieldsBlank prints a message to the


client indicating that all form fields have not been
completed.
p
r
i
n
t
("
<
t
i
t
l
e
>
A
c
c
e
s
sD
e
n
i
e
d
<
/
t
i
t
l
e
>
<
/
h
e
a
d
>
password.php
<
b
o
d
ys
t
y
l
e= \
"
f
o
n
t
f
a
m
i
l
y
:a
r
i
a
l
;
(7 of 7)

1
4
4

f
u
n
c
t
i
o
nf
i
e
l
d
s
B
l
a
n
k
(
)

1
4
5

1
4
6
1
4
7
1
4
8

f
o
n
t
s
i
z
e
:1
e
m
;c
o
l
o
r
:r
e
d
\
"
>

1
4
9

<
s
t
r
o
n
g
>

1
5
0

P
l
e
a
s
ef
i
l
li
na
l
lf
o
r
mf
i
e
l
d
s
.

1
5
1

<
b
r/
>
<
/
s
t
r
o
n
g
>
")
;
}

1
5
2
1
5
3
1
5
4

?
>
<
/
b
o
d
y
>

1
5
5<
/
h
t
m
l
>

26.6 Verifying a Username and


Password
Fig. 26.16

Verifying a username and password.

1a
c
c
o
u
n
t
1
,
p
a
s
s
w
o
r
d
1
2a
c
c
o
u
n
t
2
,
p
a
s
s
w
o
r
d
2
3a
c
c
o
u
n
t
3
,
p
a
s
s
w
o
r
d
3
4a
c
c
o
u
n
t
4
,
p
a
s
s
w
o
r
d
4
5a
c
c
o
u
n
t
5
,
p
a
s
s
w
o
r
d
5
6a
c
c
o
u
n
t
6
,
p
a
s
s
w
o
r
d
6

password.txt
(1 of 1)

7a
c
c
o
u
n
t
7
,
p
a
s
s
w
o
r
d
7
8a
c
c
o
u
n
t
8
,
p
a
s
s
w
o
r
d
8
9a
c
c
o
u
n
t
9
,
p
a
s
s
w
o
r
d
9
1
0a
c
c
o
u
n
t
1
0
,
p
a
s
s
w
o
r
d
1
0

26.7 Connecting to a Database

Databases

Store and maintain data


MySQL is a free database product
PHP supports many database operations

Access databases from Web pages

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


2

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

3
4 <!-- Fig. 26.18: data.html

-->

data.html
(1 of 2)

5 <!-- Querying a MySQL Database -->


6
7 <html xmlns = "http://www.w3.org/1999/xhtml">
8
9
10

<head>
<title>Sample Database Query</title>
</head>

11
12
13
14
15

<body style = "background-color: #F0E68C">


<h2 style = "font-family: arial color: blue">
Querying a MySQL database.
</h2>

16
17
18

<form method = "post" action = "database.php">


<p>Select a field to display:

19
20

<!-- add a select box containing options -->

21

<!-- for SELECT query

-->

<
s
e
l
e
c
tn
a
m
e= "
s
e
l
e
c
t
"
>

22
23

<
o
p
t
i
o
ns
e
l
e
c
t
e
d= "
s
e
l
e
c
t
e
d
"
>
*
<
/
o
p
t
i
o
n
>

24

<
o
p
t
i
o
n
>
I
D
<
/
o
p
t
i
o
n
>

25

<
o
p
t
i
o
n
>
T
i
t
l
e
<
/
o
p
t
i
o
n
>

26

<
o
p
t
i
o
n
>
C
a
t
e
g
o
r
y
<
/
o
p
t
i
o
n
>

27

<
o
p
t
i
o
n
>
I
S
B
N
<
/
o
p
t
i
o
n
>

Select box containing options for a SELECT


data.html
query.
(2 of 2)

<
/
s
e
l
e
c
t
>

28

<
/
p
>

29
30

<
i
n
p
u
tt
y
p
e="
s
u
b
m
i
t
"v
a
l
u
e="
S
e
n
dQ
u
e
r
y
"

31
32

s
t
y
l
e= "
b
a
c
k
g
r
o
u
n
d
c
o
l
o
r
:b
l
u
e
;

33

c
o
l
o
r
:y
e
l
l
o
w
;f
o
n
t
w
e
i
g
h
t
:b
o
l
d
"/
>

34
35

<
/
f
o
r
m
>
<
/
b
o
d
y
>

36 <
/
h
t
m
l
>

10

26.7 Connecting to a Database


Fig. 26.18

Form to query a MySQL database.

11

26.7 Connecting to a Database

Interacting with databases

SQL

Structured Query Language


Used to manipulate databases

Several useful functions

mysql_connect
mysql_select_db
mysql_query
mysql_error
mysql_fetch_row
mysql_close

12

S-ar putea să vă placă și