I have been working on a login script that will check a username and password against a MySQL database. I was using multi pages of html code and php code to handle this. I decided to look at functions() to help take away the repeated calls, but am running into problems on how to control them (I have included my script below). What I am trying to do is to have a login form appear once, the user signin, it check against database, and if unsuccessful replaces the $subtitle line in the ShowForm function with a message in the checknamepass function. When I run my code, it produes the login box, but seems to continue into the next function before waiting to get the submit. So when I try to run a loop with the FOR command, I get X copies of the login page, instead of one. Thanks in advance for the help... Tom
<HTML>
<HEAD>
<TITLE>Form Series - Example One</TITLE>
</HEAD>
<BODY>
<?PHP
$subtitle = "Account has not be activitaded Yet<br>Please try again latter or contacted the web master";
for ($n=1; $n<=3; $n++){
ShowForm($subtitle); # Display the form
$subtitle = checknamepass($formusername,$formpassword);
if ($subtitle = "FOUND"){
echo "Your logged IN !!!";
}
}
echo "You have tried to many times !!!"
exit;
function checknamepass($user_name,$user_password) {
include ("dbconnect.php");
$query = "select * from users where username = '$user_name' ";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
$login_results = mysql_fetch_array($result);
$salt = substr($user_password, 0, 2);
if ($num_results == 0 ){
$subtitle = "User does not exist, <br>please check and try again";
return $subtitle;
} elseif ($login_results["password"] != crypt($user_password, $salt)){
$subtitle = "Password is not correct, <br>please check and try again";
return $subtitle;
} elseif ($login_results["user_status"] == 0){
$subtitle = "Your account is not active yet, <br>please contact the webmaster or try again later.";
return $subtitle;
} else {
$subtitle = "FOUND";
return $subtitle;
}
}
/ Prints Login Form /
function ShowForm($subtitle) {
global $PHP_SELF;
$HTML=<<<HTML
<table width="600" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="600" height="188" valign="top">
<p></p>
<form action="$PHP_SELF" method="post" name="LOGIN" id="LOGIN">
<div align="center">
<table width="200" border="1">
<tr>
<td>
UserName
</td>
<td width="125">
<input name="formusername" type="text" id="formusername">
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
<input name="formpassword" type="password" id="formpassword">
</td>
</tr>
</table><p>
<input type="submit" name="Submit" value="Submit">
</p>
</div>
</form>
<p align="center"><font size="2"><em>$subtitle</em></font></p>
</td>
</tr>
</table>
</body>
</html>
HTML;
echo $HTML;
}
?>