我在服务器上的登录(apache)不起作用,但我确实让它在本地工作(WAMP)。我已经尝试使用我的WAMP服务器的数据库并且也可以,但是一旦文件或服务器上它停止工作。我检查了所有链接并且它们是正确的。服务器没有提供任何错误消息,它只是继续返回同一页面(not_logged_in.php)。
我使用的代码如下:
的index.php
<?php
require_once('../includes/additions/initialize.php');
require_once(FUNCTIONS);
require_once(SESSION);
// ... ask if we are logged in here:
if (logged_in()) {
// the user is logged in
include(ADDITIONS."logged_in.php");
} else {
// the user is not logged in
include(ADDITIONS."not_logged_in.php");
}
?>
logged_in()函数如下:
function logged_in() {
// confirm logged in
return isset($_SESSION['restaurant_user_id']); // restaurants
}
然后转到not_logged_in.php,其中包含以下代码:
<?php require_once(VALIDATION);?>
<?php require_once(HEADER);?>
<?php
$user_name = "";
if (isset($_POST['submit'])) {
// process the form
$required_fields = array('user_name', "password" );
validate_presences($required_fields);
if (empty($errors)) {
// attemt login
$user_name = mysql_prep($_POST["user_name"]);
$password = mysql_prep($_POST["password"]);
$found_user = attempt_login_restaurant($user_name, $password);
if ($found_user) {
// Success
// Mark user as logged in
$_SESSION["restaurant_user_id"] = $found_user["restaurant_user_id"];
$_SESSION["email_address"] = $found_user["email_address"];
$_SESSION["user_name"] = $found_user["user_name"];
$_SESSION["message"] = "Welcome ". htmlentities($_SESSION["user_name"]) . ", you are now logged in";
redirect_to(WEBSITE_ROOT. "index.php");
} else {
$_SESSION["message"] = "E-mail/password not found.";
} } }else {}
echo message();
echo form_errors($errors);
?>
<div id="mainContent" class="fluid">
<div class="restaurantphoto">
<div class="width">
<section class="login">
<form method="post" action="<?php echo WEBSITE_ROOT; ?>index.php">
<label for="user_name"><?php echo WORDING_USERNAME; ?></label><br />
<input id="user_name" type="text" name="user_name" value="<?php echo htmlentities($user_name); ?>" required /><br />
<label for="user_password"><?php echo WORDING_PASSWORD; ?></label><br />
<input id="user_password" type="password" name="password" autocomplete="off" required />
<input type="submit" name="submit" value="<?php echo WORDING_LOGIN; ?>" />
<br /><br />
</form>
<br />
<a href="<?php echo ADDONS; ?>password_reset.php"><?php echo WORDING_FORGOT_MY_PASSWORD; ?></a>
</section>
</section>
</div>
</div>
<?php include(FOOTER); ?>
这里使用的功能是:
function has_presence($value) {
// check if it is not empty
return isset($value) && $value !== "";
}
function validate_presences($required_fields) {
global $errors;
foreach ($required_fields as $field) {
$value = trim($_POST[$field]);
if (!has_presence($value)) {
$errors[$field] = fieldname_as_text($field) . " can't be blank";
} }
}
function find_restaurant_by_email($email) {
global $db;
$safe_email = mysqli_real_escape_string($db, $email);
$query = "SELECT * ";
$query .= "FROM database_restaurants ";
$query .= "WHERE email_address = '{$safe_email}' ";
$query .= "LIMIT 1";
$user_set = mysqli_query($db, $query);
// test if there is a query error
confirm_query($user_set);
if ($user = mysqli_fetch_assoc($user_set)) {
return $user;
} else {
return null;
}
}
function find_restaurant_by_user_name($user_name) {
global $db;
$safe_user_name = mysqli_real_escape_string($db, $user_name);
$query = "SELECT * ";
$query .= "FROM database_restaurants ";
$query .= "WHERE user_name = '{$safe_user_name}' ";
$query .= "LIMIT 1";
$user_set = mysqli_query($db, $query);
// test if there is a query error
confirm_query($user_set);
if ($user = mysqli_fetch_assoc($user_set)) {
return $user;
} else {
return null;
}
}
function attempt_login_restaurant($login_info, $password) {
// restaurants
$user = find_restaurant_by_email($login_info);
if (!$user) {
$user = find_restaurant_by_user_name($login_info);
}
if ($user) {
// found user, now check password
if (password_check($password, $user["password"])) {
// password matches
return $user;
} else {
return false;
}
} else {
// admin not found
return false;
}
}
function password_check($password, $existing_hash) {
// existing hash contains format and salt at start
$hash = crypt($password, $existing_hash);
if ($hash === $existing_hash) {
return true;
} else {
return false;
}
}
因为它也没有反映网站上的消息,我认为在if(空($ errors))语句之前出现了问题或者会话出现了问题(是的我确实开始了会话,它在session.php文件中)。奇怪的是,一切都在当地工作得很好。我希望你们能帮忙,这真的让我头晕目眩!!
提前致谢!