1

I am getting the error message ": session_start(): Cannot send session cache limiter - headers already sent (output started at /home/page4_insertdata.php:30) in /home/page4_insertdata.php on line 31"

I have checked for blank/white space and I can't find any, not sure what is outputting to the browser before lines 30/31.


PHP


<?php
    // Turn off all error reporting 
    error_reporting(0);
    // Report simple running errors 
    error_reporting(E_ERROR | E_WARNING | E_PARSE);
    // Reporting E_NOTICE can be good too (to report uninitialized 
    // variables or catch variable name misspellings ...) 
    error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
    // Report all errors except E_NOTICE 
    // This is the default value set in php.ini 
    error_reporting(E_ALL ^ E_NOTICE);
    // Report all PHP errors (see changelog) 
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    // Report all PHP errors 
    error_reporting(-1);
    // Same as error_reporting(E_ALL); 
    ini_set('error_reporting', E_ALL);
    ?>
<html>
    <head>
        <title>Page Form</title>
        <link rel="stylesheet" href="style.css" />
    </head>
    <body>
        <div class="container">
            <div class="main">
                <h2>PHP Form</h2><hr/>
                <?php

    session_start();

    if (isset($_POST['state'])) {

        if (!empty($_SESSION['post'])){

            if (empty($_POST['address1'])|| empty($_POST['city'])|| empty($_POST['pin'])|| empty($_POST['state'])){
                //Setting error for page 3
                $_SESSION['error_page3'] = "Mandatory field(s) are missing, Please fill it again";
                header("location: page3_form.php");
                //redirecting to third page
            } else {
                foreach ($_POST as $key => $value) {
                    $_SESSION['post'][$key] = $value;
                }

                //function to extract array
                extract($_SESSION['post']);
                //Storing values in database
                $connection = mysql_connect("localhost","my_user","my_password","my_db");
                $db = mysql_select_db("user_details", $connection);
                $query = mysql_query("insert into detail (name,email,contact,password,religion,nationality,gender,qualification,experience,address1,address2,city,pin,state) values('$name','$email','$contact','$password','$religion','$nationality','$gender','$qualification','$experience','$address1','$address2','$city','$pin','$state')", $connection);

                if ($query) {
                    echo '<p><span id="success">Form Submitted successfully..!!</span></p>';
                } else {
                    echo '<p><span>Form Submission Failed..!!</span></p>';
                }

                //destroying session
                unset($_SESSION['post']);
            }

        } else {
            header("location: page1_form.php");
            //redirecting to first page
        }

    } else {
        header("location: page1_form.php");
        //redirecting to first page
    }

    ?>
            </div>
        </div>
    </body>
</html>
jl5660
  • 91
  • 8
  • Call session_start at the beginning of the file before anything else – Harry Dec 03 '14 at 11:16
  • 1
    possible duplicate of ["Cannot send session cache limiter - headers already sent"](http://stackoverflow.com/questions/8812754/cannot-send-session-cache-limiter-headers-already-sent) – Harry Dec 03 '14 at 11:17
  • possible duplicate - http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – fortune Dec 03 '14 at 11:23

3 Answers3

1

Always try to use the session_start() function at the top of the file.

<?php
session_start();
// Turn off all error reporting 
error_reporting(0);

and remove the same from every where else, then it will work fine.

Ashique C M
  • 733
  • 4
  • 8
  • I've updated it but im still getting the same error. – jl5660 Dec 03 '14 at 11:47
  • did you remove session_start() from rest all places? – Ashique C M Dec 03 '14 at 11:51
  • I'm sure not related with session_start() now, same may with header(). – Ashique C M Dec 03 '14 at 11:54
  • move your entire code before the html tags, I mean all the calculation/ checking / database transaction etc. Never ever do the logic especially header(), session_start() etc after or in between html. use those before the html code. – Ashique C M Dec 03 '14 at 11:57
  • Its not coming up with any errors apart from the Form Submission Failed..!!, the error logs isn't working. Ive just tried to move all the php to the top of the page before the html still not luck. – jl5660 Dec 03 '14 at 12:00
  • I wont let me talk on chat until i have 20rep – jl5660 Dec 03 '14 at 12:08
  • debug ur code with print_r and die, because Im not getting what exactly the code does. – Ashique C M Dec 03 '14 at 12:11
  • I've sorted it out now the other problem was a database problem and the moving the session to the top fixed the first. – jl5660 Dec 03 '14 at 12:47
0

You have to call the session_start() command at the beginning of the file, BEFORE any output. Your HTML actually outputs before the session_start() command.

http://php.net/manual/fr/function.session-start.php

johnkork
  • 659
  • 5
  • 19
0

There's a whole block of HTML right above the session_start();

In HTML mode, everything will immediately be pushed to the browser.

Erik
  • 3,598
  • 14
  • 29