【问题标题】:Session data not being passed through to the next page会话数据未传递到下一页
【发布时间】:2016-11-22 10:54:56
【问题描述】:

我有一个有两个页面的登录系统,它非常简单,但是当我被重定向回来时,数据似乎没有通过。我尝试了几种解决方案。添加出口;然后死去;在登录的初始重定向之后。我还打印了运行良好的 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");
}

【问题讨论】:

  • 你看过 $password == $db_password 返回 true 吗?
  • 它确实像在 admin.php 页面上一样重定向,我可以回显并死掉;留在那里
  • simple login session php的可能重复
  • 您是否尝试在管理页面上回显某些内容?
  • 是的,确实有效

标签: php pdo


【解决方案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.";
}

【讨论】:

  • 这段代码有两个错误,未定义的 id 和密码
  • 啊我的错误将 $result 更改为 $row
  • 是的,我通过了,但仍然被重定向回登录页面
  • 回显 $password$db_password 并发布其值
  • 我的错误,id应该是Id。不过感谢您的回答!
猜你喜欢
  • 1970-01-01
  • 2013-05-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-28
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多