Page 1 of 1

PHP Session Start - why am I getting header errors?

Posted: Fri Jul 24, 2015 8:33 am
by simonmlewis
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/premiers/public_html/beta/ajax_length.php:2) in /home/premiers/public_html/beta/ajax_length.php on line 3
Length
When I wrote this locally, I didn't get any errors.

The user selects a value from a dropdown. That then activates a second dropdown via an Ajax call. But now, that second dropdown also causes the above warning to show.

Each Ajax dropdown is via it's own *.php file, so it has to have the session_start in it to find, and keep the session going and to find the relevant entries in the database based on the previous dropdown.

This is the file where the error is coming from:

Code: Select all

<?php
session_start();
unset($_SESSION['length']);
include "dbconn.php";
$primaryid = isset($_SESSION['primaryid']) ? $_SESSION['primaryid'] : null;
$depth = isset($_GET['depth']) ? $_GET['depth'] : null;
if ($depth == "0") { $depth = "";}

$_SESSION['depth'] = $depth;
$depth = $_SESSION['depth'];

$resultlength = mysql_query ("SELECT length FROM products WHERE primaryid = '$primaryid' AND depth = '$depth' GROUP BY length ORDER by length");
  $num_rows = mysql_num_rows($resultlength);
  if ($num_rows != 0)
  { 
  echo "<div class='product_dimension_inner'>Length</div>
  <div class='product_dimension_inner'><select id='length' name='length' onChange=\"checkthickness(this.value);\" class='selectpremier'>
  <option value='0'>Choose</option>";
    
  while ($rowlength = mysql_fetch_object($resultlength))
  {
  echo "<option value='$rowlength->length'>$rowlength->length\"</option>";
  }
  echo "</select></div>";
  }
?>

Re: PHP Session Start - why am I getting header errors?

Posted: Fri Jul 24, 2015 9:02 am
by Celauran
simonmlewis wrote:Each Ajax dropdown is via it's own *.php file, so it has to have the session_start in it to find, and keep the session going and to find the relevant entries in the database based on the previous dropdown.
You could just pass that as a parameter to your AJAX call and not worry about sessions

Re: PHP Session Start - why am I getting header errors?

Posted: Fri Jul 24, 2015 9:14 am
by simonmlewis
Maybe, but not sure how I would do that.
It's bizarre as this did definitely work. Now it seems to throw these session errors.
Why would would it throw that error though?? Is it because I am putting a PHP file in the page, via Ajax, and session_start is already in the template??
And if so, why would it not store it already, as if I take out the session, it doesn't pass it all over.

Re: PHP Session Start - why am I getting header errors?

Posted: Fri Jul 24, 2015 9:19 am
by simonmlewis
It doesn't throw this error on my local machine using XAMPP.
the version I am testing on is:
PHP Version 5.6.8

The live beta version is:
PHP Version 5.4.25

Re: PHP Session Start - why am I getting header errors?

Posted: Fri Jul 24, 2015 9:21 am
by simonmlewis
Bingo - it was a flamin " " space in the top of the PHP file!!!!
How stupid was that. Rookie mistake.

Re: PHP Session Start - why am I getting header errors?

Posted: Fri Jul 24, 2015 3:39 pm
by Christopher
I also notice that you have a ?> at the end of your file. That is generally not done in PHP only scripts because, like your error, trailing whitespace characters can cause headers to be sent in included files. These trailing characters, being whitespace, are easily missed.