我不是你所說的“經驗豐富的”PHP 程式員(仍然是學生),所以請放輕松。
我正在為一個小組專案開發登錄系統。對于使用 PHP 和 MySQL 的登錄系統,我們已經用盡了所有其他選項,因此我直接從我們正在作業的書中提取了代碼。這適用于大約 3 次登錄和注銷嘗試,它的 login.php 和 login_functions.php 處理來自表單的登錄資訊,并且應該重定向到 login.php,但現在它只是掛在一個空白的 login.php 上頁。db.php 是我的資料庫連接,它在其他任何地方都可以正常作業,所以我知道這不是破壞它的原因。
我被難住了,因為它在前幾次成功重定向,然后在我沒有接觸任何代碼的情況下就停止了。我不知所措。
登錄表單
<?php
// output errors
if (isset($errors) && !empty($errors)) {
echo '<h1>Error!</h1>
<p >The following error(s) occurred:<br>';
foreach ($errors as $msg) {
echo " - $msg<br>\n";
}
echo '</p><p>Please try again.</p>';
}
?>
<form action="login.php" method="post" id="loginform">
<h2> Login </h2>
<p><input type="email" name="email" class="inputfield" maxlength="60" placeholder="Enter your Email Address" required> </p>
<p><input type="password" name="pass" class="inputfield" maxlength="20" placeholder="Enter Your Password" required></p>
<p id="button"><input type="submit" name="submit" value="Login"></p>
<p id="register">Need an account? <strong><span class="register pink">Register</span></strong></p>
</form>
登錄功能.php
<?php
function redirect_user($page = 'index.php') {
// Start defining the URL...
// URL is http:// plus the host name plus the current directory:
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// Remove any trailing slashes:
$url = rtrim($url, '/\\');
// Add the page:
$url .= '/' . $page;
// Redirect the user:
header("Location: $url");
exit(); // Quit the script.
} // End of redirect_user() function.
function check_login($dbc, $email = '', $pass = '') {
$errors = []; // Initialize error array.
// Validate the email address:
if (empty($email)) {
$errors[] = 'You forgot to enter your email address.';
} else {
$e = mysqli_real_escape_string($dbc, trim($email));
}
// Validate the password:
if (empty($pass)) {
$errors[] = 'You forgot to enter your password.';
} else {
$p = mysqli_real_escape_string($dbc, trim($pass));
}
if (empty($errors)) { // If everything's OK.
// Retrieve the user_id and first_name for that email/password combination:
$q = "SELECT user_id, user_name FROM USERS WHERE user_email='$e' AND user_password = '$p'";
$r = @mysqli_query($dbc, $q); // Run the query.
// Check the result:
if (mysqli_num_rows($r) == 1) {
// Fetch the record:
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
// Return true and the record:
return [true, $row];
} else { // Not a match!
$errors[] = 'The email address and password entered do not match those on file.';
}
} // End of empty($errors) IF.
// Return false and the errors:
return [false, $errors];
} // End of check_login() function.
登錄.php
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Need two helper files:
require('login_functions.php');
require('db.php');
// Check the login:
list ($check, $data) = check_login($dbc, $_POST['email'], $_POST['pass']);
if ($check) { // OK!
// Set the session data:
session_start();
session_set_cookie_params(0);
$_SESSION['user_id'] = $data['user_id'];
$_SESSION['user_name'] = $data['user_name'];
// Redirect:
//redirect_user('loggedin.php');
header('Location: loggedin.php');
exit();
} else { // Unsuccessful!
// Assign $data to $errors for login_page.inc.php:
$errors = $data;
}
mysqli_close($dbc); // Close the database connection.
} // End of the main submit conditional.
?>
非常感謝那些愿意幫助我的人,我真的很感激!
uj5u.com熱心網友回復:
首先,嘗試在您的每個檔案中啟用 PHP 錯誤。stackify.com/display-php-errors/amp
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/379753.html
上一篇:SEAL全同態加密開源庫(十一) BFV-原始碼淺析(2)
下一篇:域和子域.htaccess重定向
