会话缓存限制器错误

时间:2016-03-13 18:06:31

标签: php session

我在我正在做的新演示网站上测试我的登录信息。我实际上可以登录,并且我从数据库中返回了我的用户名,但是我在顶部收到以下错误:

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/htdocs/websiteNew/testlogin.php:1) in /Applications/MAMP/htdocs/websiteNew/resources/auth/session.php on line 3

为什么我会收到错误?我知道如果我在调用会话之前调用包含文件,则可能导致错误。但是我看不到我在这里这样做了吗?

注意:更新的代码:

header.html中

   <?php
            $error='';
            if( !isset( $_SESSION ) ) session_start();

            if( !isset( $_SESSION['username'])) include('resources/auth/login.php'); 
            else exit( header('Location: testlogin.php') ); 
    ?>
  <html>
    <body>
       <!-- Navigation code -->

的index.php

<?php include 'resources/includes/header.html' ?>
      // Content Code
    </body>
  </html>

的login.php

<?php
    /* login.php */

    if( !isset( $_SESSION ) ) session_start();
    include 'resources/dbconnect/dbconfig.inc.php';

    $error = '';

    if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['submit'] ) ) {


        if( empty( $_POST['username'] ) || empty( $_POST['password'] ) ){

            $error = 'Both fields are required.';

        } else {

            $sql='SELECT `u_id`, `username` FROM `login` WHERE `username`=? AND md5(`password` )=? limit 1 ';
            $stmt=$mysqli->prepare( $sql );
            if( !$stmt ) exit('Failed to prepare sql statement');

            $username=$_POST['username']; 
            $password=md5( $_POST['password'] ); 

            $stmt->bind_param('ss', $username, $password ); 
            $res=$stmt->execute();

            $login_user = null;
            $user_name = null;

            /* bind result to variable */
            $stmt->bind_result( $login_user, $user_name );
            while( $stmt->fetch() ){
                $_SESSION['u_id'] = $login_user;
                $_SESSION['username'] = $user_name;
            }

            $stmt->close();
            $mysqli->close();

            if( isset( $_SESSION['username'] ) ) exit( header( 'location: index.php' ) );
            else $error='Incorrect username or password.';
        }
    }
?>

session.php文件

<?php

        if( !isset( $_SESSION ) ) session_start();
        if( !isset( $_SESSION['username'] ) ) exit( header('Location: index.php')  
        // Above is line 3
);
    ?>

testlogin.php

<?php 
      include 'resources/auth/session.php'; // If I only add this line I do not get any error
      include 'resources/includes/header.html'; // If I add the line I get the error in my question. But I need to include my header
    ?>

                                                          <!-- *** USER LOGGED IN ***-->
    <?php
    if ( isset( $_SESSION['username'] ) )                   
    {
    ?>
            <en><?php echo $_SESSION['username'];?></en><span class="caret"></span>
            <a href="index.php">Log Out</a>

                                                            <!-- *** USER NOT LOGGED IN ***-->

    <?php
    }
    ?>

1 个答案:

答案 0 :(得分:1)

必须在内容输出开始之前发送标头。如果您在致电session_start();之前有任何空格或类似内容,您将看到此错误消息。

发生这种情况是因为当PHP看到一个不在<?php ?>之间的空格时,它会发送标题并开始以HTML格式发送内容,并且发送标题时要迟到。

您必须确保第一个文件直接使用<?php启动,并在第一次发送所有标头时关闭。

如果你一开始就不知道你的标题(所以不能发送它),你必须使用输出缓冲(http://php.net/manual/de/ref.outcontrol.php),这意味着PHP会存储所有内容,直到脚本终止。

编辑:

也许它有助于分开

<?php include 'resources/auth/session.php' ?>

加入更多行并添加;

<?php
 include 'resources/auth/session.php';
?>

编辑2:

(也许退出按钮是一个新问题,你必须将其标记为已接受并打开一个新问题。)

您必须删除会话,例如:

将您的退出链接指向index.php?todo=logout 并添加到header.html:

<?php
    $error='';
    if( !isset( $_SESSION ) ) session_start();


    if( isset( $_GET['todo'] ) && $_GET['todo'] == 'logout'){

        //Session leeren
        session_unset();
        //Session zerstören
        session_destroy();
        //Session neu aufesetzen
        //  jetzt ist alles, was mit dem User zu tun hatte weg
        session_start();
        //Meldung
       echo 'You have been logged out!';
    }

    if( !isset( $_SESSION['username'])) include('resources/auth/login.php'); 
            else exit( header('Location: testlogin.php') ); 
    ?>
  <html>
    <body>
       <!-- Navigation code -->