1

slightly long question. I recently moved my Dev site to a web server for further testing.. and it was working 100% perfectly before the move. Most features are working except for anything involving headers..

I've added ob_start / flush to all the places I'm using headers.. and I checked that I called session_start at beginning of all pages (unless set). Played around with lot's of

things :/ But I get 1000 errors. 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /var/sites/p/******/public_html/index.php:3) in /var/sites/p/*******/public_html/index.php on line 4

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/sites/p/party******/public_html/index.php:3) in /var/sites/p/*******/public_html/index.php on line 4
Debug active

And my index.php (I've tried every way) =

<!DOCTYPE html>
<html lang="en">
<?php if (!isset($_SESSION)) {
  session_start();
  }
  ?>
<?php include ('lan-config.php')?>
<?php include (ABSPATH. 'lan-header.php') ?>
<?php 
  ?>
  <body>

I checked to see whether a session even starts, and nothing is happening. Not to mention my login script works and sets all the $_Session variables and then on refresh they dissapear.

Any ideas? I checked what I was sending in header / config and I don't even use the "Header Location" doo dah

:)

1 Answers1

0

As the error message tells you, there is already an output before the call of session_start. Because this function modify header informations, you can't make any output before. Have a look at this awesome answer to understand why.

So, change this :

<!DOCTYPE html>
<html lang="en">
<?php if (!isset($_SESSION)) {
  session_start();
  }
  ?>
<?php include ('lan-config.php')?>
<?php include (ABSPATH. 'lan-header.php') ?>
<?php 
  ?>
  <body>

To this

<?php 
if (!isset($_SESSION)) {
  session_start();
  }
?>
<!DOCTYPE html>
<html lang="en">
<?php include ('lan-config.php')?>
<?php include (ABSPATH. 'lan-header.php') ?>
<?php 
  ?>
  <body>

Maybe you will need to include lan-config.php and lan-header.php before the doctype HTML tag too, it depends of what you do in these files.

Community
  • 1
  • 1
OlivierH
  • 3,875
  • 1
  • 19
  • 32
  • Like a boss, last question. Any idea why the session does not carry through pages? E.g. My login script.. logs me in (100% gives me output and stores everything in session) and then I go back to home page and session has "unset" – UKLP Hunter Nov 23 '13 at 03:43
  • every page must have a session_start to perform $_SESSION var, if a certain page doesn't run a session_start then a $_SESSION variable is not set. Therefore you need to call session_start(); everytime you use $_SESSION var. Why is is not possible to carry through pages? for example there is a race and a 1 car already started to run while the game is not yet started how could the game be started if there's already one running? – NosiaD Nov 23 '13 at 03:54
  • Yes, you should not make `if (!isset($_SESSION)) {` test. `session_start` does not only create a new session, it resumes existing one too. – OlivierH Nov 23 '13 at 11:43
  • Gotcha, yea I wasn't calling it on my login page :) works 100% now :) – UKLP Hunter Nov 23 '13 at 14:05