SESSION数组在页面提交后不会打印内容

时间:2014-07-08 11:23:58

标签: php

我的主页面中有一个登录部分我将此部分称为分离部分,它包含以下代码:

<?php
if($_POST['submit'])
{
    $email = mysql_real_escape_string($_POST['email']);
    $password = mysql_real_escape_string($_POST['password']);
    $password = md5($password);

    $users = $GLOBALS['db']->query("SELECT * FROM users WHERE email='$email' AND password='$password'") or $GLOBALS['db']->raise_error(); // Leaving 'raise_error()' blank will create an error message with the SQL
    $users_number = $GLOBALS['db']->num_rows($users);
    if(!empty($users_number))
    {
        while($users_sql = $GLOBALS['db']->fetch_array($users))
        {
            $is_banned = $users_sql['is_banned'];
            // To check blocked users
            if($is_banned == 1)
            {
                $_SESSION['login_error_msg'] = "this users is blocked.";
                header('Location:./'); 
            }
            else
            {
                $_SESSION['first_name'] = $users_sql['first_name'];
                $_SESSION['id'] = $users_sql['id'];
                $_SESSION['logged_in'] = 'true';
                header('Location:./');
            }
        }
    }
    else
    {
        $_SESSION['login_error_msg'] = "Wrong username or password";
        header('Location:./'); 
    }
}
else
{
?>
<table class="fast_login_container">
    <tr>
        <td>
            <form name="login_form" action="./" method="post">
            <table class="fast_login" border="0">
                <tr>
                    <colspan="3">
                        <?php
                            if($_SESSION['login_error_msg'])
                            {
                                echo $_SESSION['login_error_msg'];
                            }
                        ?>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input name="email" type="email" placeholder="Kullanıcı adı" />
                    </td>

                    <td>
                        <input name="password" type="password" placeholder="Parola"/>
                    </td>
                    <td >
                        <input name="submit" type="submit" value="Giriş"/>
                    </td>

                </tr>
            </table>
            </form>
        </td>
    </tr>
</table>
<?php
}
?>

此代码创建登录表单,当用户输入电子邮件和密码时,我会在php部分的同一页面中检查它们。 我的问题如果我有错误的电子邮件或密码,我应该显示一条错误消息,我将错误消息保存在会话变量中,并在重定向到主页面(登录表单所在的位置)后在html部分中调用它。 我无法显示错误消息。我打印会话数组befor标题(&#39;位置:./')和之后;因为它打印错误信息后,它不打印任何东西。 PS:session_start()位于头文件中,我将其包括在内:

<?php
session_start();
?>
<body>
    <table class="body_table" border="1">
        <tr><td><?php include_once("include-parts/header_parts.php")?></td></tr> 

登录部分位于header_parts.php。

我应该在这里编辑什么来获取错误消息???

1 个答案:

答案 0 :(得分:0)

问题是php无法在重定向到其他页面之前设置会话。您应该在每个标头(“”)函数之前使用session_write_close()。 e.g。

            $_SESSION['first_name'] = $users_sql['first_name'];
            $_SESSION['id'] = $users_sql['id'];
            $_SESSION['logged_in'] = 'true';

            session_write_close();

            header('Location:./');
相关问题