我在PHP的登录脚本中有以下代码。当我输入登录所需的电子邮件和密码时,页面刷新而不是将我重定向到page.php。最初需要从不同的表中选择数据的代码,例如等级。如果查询字符串更改回上一个表名,则代码可以正常工作。现在我有一个不同的表设置,并认为更改查询中的表名应该做的工作,但它不是。请帮忙。
<?php
include ('database_connection.php');
if (isset($_POST['formsubmitted'])) {
// Initialize a session:
session_start();
$error = array(); //this aaray will store all error messages
if (empty($_POST['e-mail'])) {//if the email supplied is empty
$error[] = 'You forgot to enter your Email ';
} else {
if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['e-mail'])) {
$Email = $_POST['e-mail'];
} else {
$error[] = 'Your Email Address is invalid ';
}
}
if (empty($_POST['Password'])) {
$error[] = 'Please Enter Your Password ';
} else {
$Password = $_POST['Password'];
}
if (empty($error)) {//if the array is empty , it means no error found
$query_check_credentials = "SELECT * FROM enroll WHERE (email='$Email' AND password='$Password')";
$result_check_credentials = mysqli_query($dbc, $query_check_credentials);
if (!$result_check_credentials) {//If the QUery Failed
echo 'Query Failed ';
}
if (@mysqli_num_rows($result_check_credentials) == 1) {//if Query is successfull // A match was made.
$_SESSION = mysqli_fetch_array($result_check_credentials, MYSQLI_ASSOC); //Assign the result of this query to SESSION Global Variable
header("Location:pageX.php");
exit();
} else {
echo $email;
echo $password;
$msg_error = 'Either Email address or Password is Incorrect';
}
} else {
echo '<div class="errormsgbox"> <ol>';
foreach ($error as $key => $values) {
echo ' <li>' . $values . '</li>';
}
echo '</ol></div>';
}
if (isset($msg_error)) {
echo '<div class="warning">' . $msg_error . ' </div>';
}
/// var_dump($error);
} // End of the main Submit conditional.
?>
答案 0 :(得分:2)
重定向不起作用,因为Location:
标头的值必须是绝对URI,即它必须以协议开头。 pageX.php
是一个相对URI,虽然有些浏览器尽力猜测程序员想要什么,但其他人只是坚持标准,正如你所说,不会重定向。
尝试这样的事情:
header('Location: http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/pageX.php');
它会像魅力一样发挥作用。