Troubleshooting “Warning: session_start(): Cannot send session cache limiter - headers already sent”

后端 未结 15 1676
醉酒成梦
醉酒成梦 2020-11-28 14:34

i am getting Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at error

<
相关标签:
15条回答
  • 2020-11-28 15:01

    This should solve your problem. session_start() should be called before any character is sent back to the browser. In your case, HTML and blank lines were sent before you called session_start(). Documentation here.

    To further explain your question of why it works when you submit to a different page, that page either do not use session_start() or calls session_start() before sending any character back to the client! This page on the other hand was calling session_start() much later when a lot of HTML has been sent back to the client (browser).

    The better way to code is to have a common header file that calls connects to MySQL database, calls session_start() and does other common things for all pages and include that file on top of each page like below:

    include "header.php";
    

    This will stop issues like you are having as also allow you to have a common set of code to manage across a project. Something definitely for you to think about I would suggest after looking at your code.

    <?php
    session_start();
    
                    if (isset($_SESSION['error']))
    
                    {
    
                        echo "<span id=\"error\"><p>" . $_SESSION['error'] . "</p></span>";
    
                        unset($_SESSION['error']);
    
                    }
    
                    ?>
    
                    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    
                    <p>
                     <label class="style4">Category Name</label>
    
                       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="categoryname" /><br /><br />
    
                        <label class="style4">Category Image</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    
                        <input type="file" name="image" /><br />
    
                        <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    
                       <br />
    <br />
     <input type="submit" id="submit" value="UPLOAD" />
    
                    </p>
    
                    </form>
    
    
    
    
                                 <?php
    
    
    
    require("includes/conn.php");
    
    
    function is_valid_type($file)
    
    {
    
        $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif", "image/png");
    
    
    
        if (in_array($file['type'], $valid_types))
    
            return 1;
    
        return 0;
    }
    
    function showContents($array)
    
    {
    
        echo "<pre>";
    
        print_r($array);
    
        echo "</pre>";
    }
    
    
    $TARGET_PATH = "images/category";
    
    $cname = $_POST['categoryname'];
    
    $image = $_FILES['image'];
    
    $cname = mysql_real_escape_string($cname);
    
    $image['name'] = mysql_real_escape_string($image['name']);
    
    $TARGET_PATH .= $image['name'];
    
    if ( $cname == "" || $image['name'] == "" )
    
    {
    
        $_SESSION['error'] = "All fields are required";
    
        header("Location: managecategories.php");
    
        exit;
    
    }
    
    if (!is_valid_type($image))
    
    {
    
        $_SESSION['error'] = "You must upload a jpeg, gif, or bmp";
    
        header("Location: managecategories.php");
    
        exit;
    
    }
    
    
    
    
    if (file_exists($TARGET_PATH))
    
    {
    
        $_SESSION['error'] = "A file with that name already exists";
    
        header("Location: managecategories.php");
    
        exit;
    
    }
    
    
    if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
    
    {
    
    
    
        $sql = "insert into Categories (CategoryName, FileName) values ('$cname', '" . $image['name'] . "')";
    
        $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
    
      header("Location: mangaecategories.php");
    
        exit;
    
    }
    
    else
    
    {
    
    
    
    
    
        $_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";
    
        header("Location: mangagecategories.php");
    
        exit;
    
    }
    
    ?> 
    
    0 讨论(0)
  • 2020-11-28 15:02

    For others who may run across this - it can also occur if someone carelessly leaves trailing spaces from a php include file. Example:

     <?php 
        require_once('mylib.php');
        session_start();
     ?>
    

    In the case above, if the mylib.php has blank spaces after its closing ?> tag, this will cause an error. This obviously can get annoying if you've included/required many files. Luckily the error tells you which file is offending.

    HTH

    0 讨论(0)
  • 2020-11-28 15:04

    This started for me when I redirected my site to https:// (for SSL Certificate). From my experience with this issue, the session_start() had to be before the browser saw any HTML code. For my example, I was using session_start() in nav.php to determine the options of the navigation bar. I ended up placing session_start() right after the php comments in the index.php file and every page which called nav.php. I was able to keep the php comments before session_start() but could not keep the HTML comments above the php.

    0 讨论(0)
  • 2020-11-28 15:05

    I was able to solve similar Warning: session_start(): Cannot send session cache limiter - headers already sent by just removing a space in front of the <?php tag.

    It worked.

    0 讨论(0)
  • 2020-11-28 15:07

    replace session_start(); with @session_start(); in your code

    0 讨论(0)
  • 2020-11-28 15:11

    I had the same issue, but my solution wasn't as obvious as the suggested ones. It turned out that my php-file was written in UTF-8, which caused issues. I copy/pasted the content of the entire file into a new php-file (Notepad++ tells me this is written in ANSI rather than UTF-8), and now it work flawlessly.

    0 讨论(0)
提交回复
热议问题