我有一个处理用户登录的PHP脚本。在此脚本中有3个表来查询和检查人员何时登录(以检查此人是用户,雇主还是管理员)。我只是使用if()else()来连续查询3表。它有效,但有许多重复的代码。
现在我想提取那些重复的代码块并将它们放入一个函数中,但由于原来的3个表查询嵌套在if ... else ...中,如何将代码块拉入函数中?这是否称为递归函数?
这是脚本:
<?php # Script - login.php
// This is the login page for the site.
require ('includes/config.inc.php');
$page_title = 'Login';
include (HEADER);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require (MYSQL);
// Login Validation: email + password
// Trim all the incoming data:
$trimmed = array_map('trim', $_POST);
// Validate the email address:
if (filter_var($trimmed['email'], FILTER_VALIDATE_EMAIL)) {
$e = mysqli_real_escape_string($dbc, $trimmed['email']);
} else {
$e = FALSE;
echo '<p class="error">You forgot to enter your email address!</p>';
}
// Validate the password:
if (preg_match('/^\w{4,20}$/', $trimmed['pass'])) { //Here the password input name is pass in the form below!!
$p = mysqli_real_escape_string($dbc, $trimmed['pass']);
} else {
$p = FALSE;
echo '<p class="error">You forgot to enter your password!</p>';
}
if ($e && $p) { // If email and password are validated.
// First query the users table:
$q = "SELECT user_id, first_name, DATE_FORMAT(last_login_time, '%a, %b %e at %l:%i%p') as f_last_login_time, last_login_time FROM users WHERE (email='$e' AND pass=SHA1('$p')) AND active IS NULL";
$r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
if (@mysqli_num_rows($r) == 1) { // A match was made in the user table
// Register the values:
$_SESSION = mysqli_fetch_array($r, MYSQLI_ASSOC);
// Update last_login_time column when a User logs in
$q = "UPDATE users SET last_login_time=NOW()";
$r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br>MySQL Error: " . mysqli_error($dbc));
$test_affected_rows = mysqli_affected_rows($dbc); // get around debug issue
if ($test_affected_rows != 1) {
echo '<p class="error">There is some system error, please contact administrator!</p>';
}
mysqli_free_result($r);
mysqli_close($dbc);
// Redirect the user to loggedin_user.php page:
$url = BASE_URL . 'loggedin_user.php'; // Define the URL.
ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.
} else { // Continue querying the employers table.
$q = "SELECT employer_id, first_name, DATE_FORMAT(last_login_time, '%a, %b %e at %l:%i%p') as f_last_login_time, last_login_time FROM employers WHERE (email='$e' AND pass=SHA1('$p')) AND active IS NULL";
$r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
if (@mysqli_num_rows($r) == 1) { // A match was made in the employers table
// Register the values:
$_SESSION = mysqli_fetch_array($r, MYSQLI_ASSOC);
// Update last_login_time column when an Employer logs in
$q = "UPDATE employers SET last_login_time=NOW()";
$r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br>MySQL Error: " . mysqli_error($dbc));
$test_affected_rows = mysqli_affected_rows($dbc); // get around debug issue
if ($test_affected_rows != 1) {
echo '<p class="error">There is some system error, please contact administrator!</p>';
}
mysqli_free_result($r);
mysqli_close($dbc);
// Redirect the user to loggedin_employer.php page:
$url = BASE_URL . 'loggedin_employer.php'; // Define the URL.
ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.
} else { // Continue querying the administrators table.
// Administrators table doesn't contain the Active field, as no registration process needed.
$q = "SELECT admin_id, first_name, DATE_FORMAT(last_login_time, '%a, %b %e at %l:%i%p') as f_last_login_time, last_login_time FROM administrators WHERE (email='$e' AND pass=SHA1('$p'))";
$r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
if (@mysqli_num_rows($r) == 1) { // A match was made in the administrator table
// Register the values:
$_SESSION = mysqli_fetch_array($r, MYSQLI_ASSOC);
// Update last_login_time column when an Administrator logs in
$q = "UPDATE administrators SET last_login_time=NOW()";
$r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br>MySQL Error: " . mysqli_error($dbc));
$test_affected_rows = mysqli_affected_rows($dbc); // get around debug issue
if ($test_affected_rows != 1) {
echo '<p class="error">There is some system error, please contact administrator!</p>';
}
mysqli_free_result($r);
mysqli_close($dbc);
// Redirect the user:
// Redirect the user to loggedin_admin.php page:
$url = BASE_URL . 'loggedin_admin.php'; // Define the URL.
ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.
} else { // If email and password were not validated
echo '<p class="error">The email or password you entered is not correct. Please try again.</p>';
}
}
}
}
mysqli_close($dbc);
} // End of SUBMIT conditional.
?>
<h1>Login</h1>
<p>Your browser must allow cookies in order to log in.</p>
<form action="login.php" method="post">
<fieldset>
<p><b>Email Address:</b> <input type="text" name="email" size="20" maxlength="60" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></p>
<p><b>Password:</b> <input type="password" name="pass" size="20" maxlength="20" value="<?php if (isset($_POST['pass'])) echo $_POST['pass']; ?>" /></p>
<div align="center"><input type="submit" name="submit" value="Login" /></div>
</fieldset>
</form>
<?php include (FOOTER); ?>
我尝试制作这样的函数:
function login_process($q_check_table, $q_update_last_login){
// First query the users table:
$q1 = $q_check_table;
$r = mysqli_query($dbc, $q1) or trigger_error("Query: $q1\n<br />MySQL Error: " . mysqli_error($dbc));
if (@mysqli_num_rows($r) == 1) { // A match was made in the user table
// Register the values:
$_SESSION = mysqli_fetch_array($r, MYSQLI_ASSOC);
// Update last_login_time column when a User logs in
$q2 = $q_update_last_login;
$r = mysqli_query($dbc, $q2) or trigger_error("Query: $q2\n<br>MySQL Error: " . mysqli_error($dbc));
$test_affected_rows = mysqli_affected_rows($dbc); // get around debug issue
if ($test_affected_rows != 1) {
echo '<p class="error">There is some system error, please contact administrator!</p>';
}
}
}
但是由于嵌套if ... else ....,这似乎不对。