PHP Session Start - why am I getting header errors?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

PHP Session Start - why am I getting header errors?

Post 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>";
  }
?>
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

Bingo - it was a flamin " " space in the top of the PHP file!!!!
How stupid was that. Rookie mistake.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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.
(#10850)
Post Reply