会话数据未传递到下一页

时间:2016-07-19 13:41:32

标签: php pdo

我有一个有两个页面的登录系统,它很简单,但是当我被重定向回来时,似乎没有传递数据。我尝试了几种解决方案。添加退出;死从登录后的初始重定向。我还打印了一个工作正常的session_id。

登录页面:

session_start();

if(isset($_POST['login'])){
    $username = strip_tags($_POST['username']);
    $password = strip_tags($_POST['password']);

    $username = stripslashes($username);
    $password = stripslashes($password);

    $password = md5($password);

    $sql = "SELECT * FROM table WHERE username = :username LIMIT 1";
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(":username", $username);
    $stmt->execute();
    foreach($stmt as $row) {
        $id = $row['id'];
        $db_password = $row['password'];
    }

    if($password == $db_password){
        $_SESSION['username'] = $username;
        $_SESSION['id'] = $id;
        header("Location: admin.php");
        exit;
    }else{
        echo "You did not enter the correct details.";
    }
}

这是管理页面。它只是重定向回登录,并没有进一步下载

session_start();

if(!isset($_SESSION['id'])){
    header("Location: login.php");
}

1 个答案:

答案 0 :(得分:1)

<强> execute()

  

成功时返回TRUE,失败时返回FALSE。

您需要从结果集中获取数据

 $stmt->execute();
 $row = $stmt->fetch(PDO::FETCH_ASSOC);// fetch data
 $id = $row['id'];
 $db_password = $row['password'];
 if($password == $db_password){// then compair
    $_SESSION['username'] = $username;
    $_SESSION['id'] = $id;
    header("Location: admin.php");
    exit;
}else{
    echo "You did not enter the correct details.";
}