PHP记住我重定向

时间:2018-02-05 14:25:22

标签: php mysql session cookies

我是PHP的初学者。我正在使用登录 / 密码记住我创建PHP登录表单, 我的问题是它被重定向到userarea.php

即使我没有点击记住我(复选框)。

我不知道问题是关于cookie还是变量。我尝试了很多解决方案而没有任何问题。

这是我的代码:

    session_start();


    //checking if remember me == true

function loggedin()
{   
    if(isset($_SESSION["email"]) || isset($_COOKIE["email"]))
    {
        $loggedin = true;
        return $loggedin;
    }
}

if(loggedin())
{
    header("Location: userarea.php");
}

if(isset($_POST['submit'])){

require 'db.php';

$email = mysqli_real_escape_string($connection,$_POST["email"]);
$password = mysqli_real_escape_string($connection,$_POST["password"]);

$query = "SELECT * FROM users WHERE email = '$email' and pwd='$password'";
$result = mysqli_query($connection,$query);

if (!$query)
{
    die('Error: ' . mysqli_error($connection));
}
$count = mysqli_num_rows($result);
    if($count > 0){

    $row = mysqli_fetch_assoc($result);
    $_SESSION["email"] = $row["email"];
    $_SESSION["civile"] = $row["civile"];
    $_SESSION["nom"] = $row["nom"];
    $_SESSION["prenom"] = $row["prenom"];
    $_SESSION["address"] = $row["address"];
    $_SESSION["city"] = $row["city"];
    $_SESSION["zip"] = $row["zip"];
    $_SESSION["telephone"] = $row["telephone"];

    //saving checkbox into variable

    if(isset($_POST['rememberme'])){
        setcookie("email", $email, time()+7200);
    }


    echo '<div class="alert alert-success">
    Hi '.$_SESSION["nom"].' ! you will be redirected in few seconds.
    </div>';
}else{

    echo '<div class="label label-warning">
    Username or password are incorrect.
    </div>';

}

}

以下是HTML代码:

        <input id="email" type="text" size="40" class="form-control" name="email" placeholder="Email">


        <input id="password" type="password" size="40" class="form-control" name="password" placeholder="Password">

    <input class="btn btn-success" type="submit" value="Submit" name="submit">


<td> <input type="checkbox" name="rememberme" id="rememberme" value="1"> 
    <label> Remember me  </label>

1 个答案:

答案 0 :(得分:0)

您还在检查会话中的电子邮件,这是您一直在设置的原因,这就是您遇到此问题的原因。

   session_start();


    //checking if remember me == true

function loggedin()
{   
    if(isset($_COOKIE["email"]))
    {
        $loggedin = true;
        return $loggedin;
    }
}

if(loggedin())
{
    header("Location: userarea.php");
}

if(isset($_POST['submit'])){

require 'db.php';

$email = mysqli_real_escape_string($connection,$_POST["email"]);
$password = mysqli_real_escape_string($connection,$_POST["password"]);

$query = "SELECT * FROM users WHERE email = '$email' and pwd='$password'";
$result = mysqli_query($connection,$query);

if (!$query)
{
    die('Error: ' . mysqli_error($connection));
}
$count = mysqli_num_rows($result);
    if($count > 0){

    $row = mysqli_fetch_assoc($result);
    $_SESSION["email"] = $row["email"];
    $_SESSION["civile"] = $row["civile"];
    $_SESSION["nom"] = $row["nom"];
    $_SESSION["prenom"] = $row["prenom"];
    $_SESSION["address"] = $row["address"];
    $_SESSION["city"] = $row["city"];
    $_SESSION["zip"] = $row["zip"];
    $_SESSION["telephone"] = $row["telephone"];

    //saving checkbox into variable

    if(isset($_POST['rememberme'])){
        setcookie("email", $email, time()+7200);
    }


    echo '<div class="alert alert-success">
    Hi '.$_SESSION["nom"].' ! you will be redirected in few seconds.
    </div>';
}else{

    echo '<div class="label label-warning">
    Username or password are incorrect.
    </div>';

}

}