存储会话后&回应它,没有任何表现

时间:2013-03-24 16:03:57

标签: php session pdo

我有一个表单可以将一些东西插入到MySQL数据库中。

用户名。 密码。 ID。

我将用户名存储到会话中,然后我更改标题(.php?recovery = success)。 在那之后,我想测试我的会话是否有效(以后再使用,那么我可以转到数据库中的用户名列来获取我们插入的ID,如果我只是回显$ ID,它将生成一个新的ID

else if (isset($_GET['recovery']) && $_GET['recovery'] == 'success') 
{
echo $_SESSION['user'];

    /* 

    ***REMOVED FOR TESTING***

    $fetch = $connect->query("SELECT * FROM users WHERE username = ':username' LIMIT 1");
    $fetch->bindValue(':username', $_SESSION['user']);
    $fetch->execute();

    while($row = $fetch->fetch( PDO::FETCH_ASSOC )) {
        echo $row['recover_id'];
    }
    */
}

问题:

我填写了表格,提交后它带我去恢复.php?恢复=成功 然后在回显会话数据时,没有输出?

我需要文件session.inc.php,它会打开一个新会话。

这是整个代码:

http://pastebin.com/ba77rDi3

我做错了什么?

我是PHP新手,特别是PDO。 谢谢!

附加:

Session.inc.php

<?php
session_start();
?>

2 个答案:

答案 0 :(得分:2)

看起来您的代码正在将信息存储到会话中,然后通过调用标头重定向(请参阅代码段)。

                // Let's store these into a session now.
                $_SESSION['user'] = $username;
                $_SESSION['pass'] = $password;

                //Now let's refresh the page, to a different header.
                header('Location: recover.php?recovery=success');

要谨慎;我建议明确致电

session_write_close()

在重定向之前,确保在执行之前正确保存会话数据离开当前页面......如下所示:

                    // Let's store these into a session now.
                    $_SESSION['user'] = $username;
                    $_SESSION['pass'] = $password;


                    session_write_close();                       

                    //Now let's refresh the page, to a different header.
                    header('Location: recover.php?recovery=success');

答案 1 :(得分:1)

问题是:

我把代码放在声明中,检查恢复是否设置

if (!isset($_GET['recovery'])) {

在代码的顶部,如果你没有在pastebin中看到。

我把代码放在那个声明之外,它起作用了。

相关问题