View
234
Download
0
Category
Preview:
Citation preview
8/10/2019 SynapseIndia PHP Fuction Development
1/12
1
50 $userVerified = 0;
51
52 // read each line in file and check username
53 // and password
54 while ( feof( $file ) && $userVerified ) {
55
56// read line from file
57$line = fgets( $file, 255);
58
59 // remove newline character from end of line
60 $line = chop( $line );
61
62 // split username and password
63 $field = split( ",", $line, 2);
64
65// verify username
66if( $USERNAME == $field[ 0] ) {
67 $userVerified = 1;
68
69// call function checkPassword to verify
70 users password
71if( checkPassword( $PASSWORD, $field )
72== true)
73 accessGranted( $USERNAME );
74 else
75 wrongPassword();
password.php
(3 of 7)
Before entering the whileloop, variable
$userVerifiedis set to 0.
The whileloop executes as long as the there are morelines in the file to read and variable $userVerifiedis
still 0or empty.
Function fgetsreads a line from the text file.
The result is assigned to variable $line.
Function chopremoves the newline character
from the end of the line.
Function splitis called to separate the string at the
specified delimiter (in this case, a comma). The
resulting array is stored in array $field.
The username entered by the user is tested
against the one returned in the text file (stored
in the first element of the array). If they match,
variable $userVerifiedis set to 1.
Function checkPasswordis called to verify the
users password. Variable $PASSWORDand array
$fieldare passed to the function.
If function checkPasswordreturns true, function
accessGrantedis called to notify the client thatpermission has been granted. Otherwise, function
wrongPasswordis called.
8/10/2019 SynapseIndia PHP Fuction Development
2/12
2
76}
77 }
78
79 // close text file
80 fclose( $file );
81
82// call function accessDenied if username has
83// not been verified
84 if( $userVerified )
85accessDenied();
86 }
87
88 // verify user password and return a boolean
89 functioncheckPassword( $userpassword, $filedata )
90{
91 if( $userpassword == $filedata[ 1] )
92returntrue;
93 else
94 returnfalse;
95 }
96
password.php
(4 of 7)
After the whileloop has executed, function
fcloseis called to close the file.
If variable $userVerifiedhas not been set to a
value other than 0, function accessDeniedis
called to notify the client that access has been
denied.
Function checkPasswordcompares the users
password to the password in the file. If they match,
trueis returned, whereas falseis returned if they
do not.
8/10/2019 SynapseIndia PHP Fuction Development
3/12
3
97 // print a message indicating the user has been added
98 functionuserAdded( $name )
99 {
100 print( "Thank You
101
103 You have been added
104 to the user list, $name.
105
Enjoy the site.");
106 }
107
108 // print a message indicating permission
109 // has been granted
110functionaccessGranted( $name )
111 {
112 print( "Thank You
113
115 Permission has been
116 granted, $name.
117 Enjoy the site.");
118 }
119
password.php
(5 of 7)Function userAddedprints a message to the
client indicating that the user has been added.
Function accessGrantedprints a
message to the client indicating that
permission has been granted.
8/10/2019 SynapseIndia PHP Fuction Development
4/12
4
120 // print a message indicating password is invalid
121functionwrongPassword()
122{
123print( "Access Denied
124
126 You entered an invalid
127 password.
Access has
128 been denied.");
129 }
130
131 // print a message indicating access has been denied
132 functionaccessDenied()
133{
134print( "Access Denied
135
137
138 You were denied access to this server.
139
" );
140 }
141
password.php
(6 of 7)
Function wrongPasswordprints a message to the
client indicating that the password is invalid.
Function accessDeniedprints a message to the
client indicating that access has been denied.
8/10/2019 SynapseIndia PHP Fuction Development
5/12
5
142 // print a message indicating that fields
143// have been left blank
144 functionfieldsBlank()
145{
146 print( Access Denied
147
149
150 Please fill in all form fields.
151
);
152 }
153 ?>
154
155
password.php
(7 of 7)
Function fieldsBlankprints a message to the
client indicating that all form fields have not been
completed.
8/10/2019 SynapseIndia PHP Fuction Development
6/12
6
26.6 Verifying a Username and
PasswordFig. 26.16 Verifying a username and password.
8/10/2019 SynapseIndia PHP Fuction Development
7/12
7
password.txt
(1 of 1)
1account1 password1
2account2 password2
3account3 password3
4account4 password4
5account5 password5
6account6 password6
7account7 password7
8account8 password8
9account9 password9
10account10 password10
8/10/2019 SynapseIndia PHP Fuction Development
8/12
8
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
8/10/2019 SynapseIndia PHP Fuction Development
9/12
9
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 -->
5 < -- Querying a MySQL Database -->
6
7
8
9 Sample Database Query
10
11
12
13
14Querying a MySQL database.
15
16
17
18
Select a field to display:
19
20< -- add a select box containing options -->
21 < -- for SELECT query -->
data.html
(1 of 2)
22
8/10/2019 SynapseIndia PHP Fuction Development
10/12
10
22
23 *
24 ID
25 Title
26 Category
27 ISBN
28
29
30
31
34
35
36
data.html
(2 of 2)
Select box containing options for a SELECT
query.
8/10/2019 SynapseIndia PHP Fuction Development
11/12
11
26.7 Connecting to a DatabaseFig. 26.18 Form to query a MySQL database.
8/10/2019 SynapseIndia PHP Fuction Development
12/12
12
26.7 Connecting to a Database
Interacting with databases SQL
Structured Query Language
Used to manipulate databases
Several useful functionsmysql_connect
mysql_select_db
mysql_query
mysql_errormysql_fetch_row
mysql_close
Recommended