我正在创建一个登录脚本,并有问题,让用户登录并直接到正确的页面。目前,用户提交登录表单,该表单指向“安全”页面,该页面检查会话匹配,如下所示,但它似乎一直将我踢出去。
这是我的代码-
Login.php
session_start();
include("clientarea/inc/config.php"); //Establishing connection with our database
$error = ""; //Variable for storing our errors.
if(isset($_POST["login"])){
if(empty($_POST["email"]) || empty($_POST["password"])){
$error = "Both fields are required";
} else {
// Define $email and $password
$email = $_POST['email'];
$password = $_POST['password'];
// To protect from MySQL injection
$email = mysqli_real_escape_string($connection, $email);
$password = mysqli_real_escape_string($connection, $password);
//$password = md5($password);
//Check username and password from database
$stmt = "SELECT * FROM users WHERE email='$email' and password='$password'";
$result = mysqli_query($connection, $stmt);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
//If username and password exist in our database then create a session.
if(mysqli_num_rows($result) == 1){
$_SESSION['email'] = $login_user; // Initializing Session
header("location: clientarea/index.php"); // Redirecting To Other Page
} else {
$error = "Incorrect username or password.";
}
}
}如你所见,在验证之后,它会检查是否有用户,然后转到“安全”页面。
这是auth.php
include('inc/config.php');
session_start();
$user_check = $_SESSION['email'];
$sql = mysqli_query($connection, "SELECT email FROM users WHERE email='$user_check' ");
$row = mysqli_fetch_array($sql,MYSQLI_ASSOC);
$login_user = $row['email'];
if(!isset($user_check))
{
header("Location: http://localhost/findpt/index.php");
}现在,auth.php在“安全”页面的顶部运行,以确保用户已登录,否则将重定向到主页。现在,如果我删除了auth.php文件,我就会直接转到“安全”页面,但有了它,它就会引导我转到主页。
如果我回显$_SESSION‘’email‘,我什么也收不到?
发布于 2017-08-05 07:45:32
如果我没看错的话,login.php是一种将用户重定向到安全页面auth.php的表单。
纵观login.php,$login_user似乎从未初始化过。所以你给$_SESSION‘’email‘分配了一个未赋值的变量。
此外,也许可以在login.php if语句中创建另一个条件来检查重复登录,只是为了覆盖您的P&Q。
https://stackoverflow.com/questions/45516509
复制相似问题