donglong2856 2014-10-08 17:07
浏览 25

我正在尝试验证$ _SESSION的状态以允许访问网页,但它无法正常工作

The page that has the following code will only redirect:

Here is the code I am trying to include at the top of other pages that I want to check S_SESSION before allowing access:

<?php
session_start();
include ('connect_SQL_DB.php');
include('header.html');

if(!isset($_SESSION['login']) || (trim($_SESSION['login'])=='')) {
header('location:login_redirect.php');
exit();
} 
?>

Here js my code for the login page:


<?php
session_start();
include ('connect_SQL_DB.php'); 
include('header.html');

print '<h2>Login Form</h2>
    <p>Users who are logged in can view and make changes to the website.</p>';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    if ( (!empty($_POST['email'])) && (!empty($_POST['password'])) ) {

        $query=mysql_query("SELECT * FROM account WHERE email= '"
        . $_POST['email'] . "'

        AND password= '" . $_POST['password'] . "'

        AND enabled='1'") or die (mysql_error());

        $check = mysql_num_rows($query);

        if ($check > 0) {

            $row = mysql_fetch_array($query, MYSQL_ASSOC);

                $user = array(
                'userid' => $row['userid'],
                'email' => $row['email'],
                'accesslevel' => $row['accesslevel'],
                'password' => md5 ($row['password']), 
                'enabled' => $row['enabled'],
                );      



            //Session
            session_start();
            $_SESSION['login'][] = $user;
            header('location:message.php');
            ob_end_clean();
            exit();

        } else {

            header('location:login_redirect.php');



        }

    } else {

    print '<p>Please make sure you enter both an email address and a password!<br />Go
    back and try again.</p>';

    }

} else {

    print '<form action="login.php" method="post">
    <p>Email Address: <input type="text" name="email" size="20" /></p>
    <p>Password: <input type="password" name="password" size="20" /></p>
    <p><input type="submit" name="submit" value="Log In!" /></p>
    </form>';

}

include('footer.html');
?>
  • 写回答

1条回答 默认 最新

  • dongxun6458 2014-10-08 17:58
    关注

    Its not working because header already sent when you include header.html

    Always put your redirect logic on above any print statement.

    Try to put following code on every page.

    <?php
    session_start();
    if(!isset($_SESSION['login']) || (trim($_SESSION['login'])=='')) {
    header('location:login_redirect.php');
    exit();
    }
    include ('connect_SQL_DB.php');
    include('header.html');
    
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?