0

I would like to separate my source folders into two: The folders that contain the code that you type into the address bar and those that make up parts of the page (tiles) and other code (classes, etc). So at the start of every php file I added:

<?php
// index.php
include("config.php");
include("session.php");
?>

Config contains just this so far, but allows me to expand if I need other directories (logs, etc.)

<?php
// config.php
$_PATHS["base"]      = dirname(dirname(__FILE__)) . "\\";
$_PATHS["includes"]  = $_PATHS["base"] . "includes\\";
ini_set("include_path", "$_PATHS[includes]");
?>

And session has amongst other things, in the constructor, a call to session_start. It also requires other classes which are included elsewhere - which necessitates the config being listed before the session inclusion. However I get the error

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started ...

If I switch the includes around that particular error goes away but I need to start manually munging the links to the header files. Is there anyway of setting the directories first and still being able to use sessions or must session_start be the very first thing the a file includes?

Paolo Bergantino
  • 449,396
  • 76
  • 509
  • 431
graham.reeds
  • 15,267
  • 16
  • 66
  • 133
  • BTW, why the double-call to dirname()? – Bobby Jack Jul 13 '09 at 22:21
  • I saw several (I can find two now) web-sites listing that way so I decided that is must be 'best practice' and copied without really knowing why. I am learning php4 (and before anyone says php4 is dead, well not on my web-server it isn't!). – graham.reeds Jul 13 '09 at 22:24

2 Answers2

3

The rest of that error is the exact bit that will tell you where the problem is! Chances are you have some trailing whitespace at the end of config.php.

(Either that, or session.php sends output before your call to session_start(), but I'm really just guessing now :)

Bobby Jack
  • 14,812
  • 10
  • 59
  • 94
  • The rest of that error is just the error path - and I know what that is - the config file. I wouldn't of thought that ini_set would cause the file output to begin - I would of thought it would occur further down the line with maybe the doctype or html tag. – graham.reeds Jul 13 '09 at 22:29
1

I don't know if you have already tried this, but as a means of testing remove the config.php include and paste the config code in there instead.

So this:

<?php
// index.php
include("config.php");
include("session.php");
?>

becomes this:

<?php
// config
$_PATHS["base"]      = dirname(dirname(__FILE__)) . "\\";
$_PATHS["includes"]  = $_PATHS["base"] . "includes\\";
ini_set("include_path", "$_PATHS[includes]");
//index
include("session.php");
?>

If it works then you have a problem with your config.php file* [see below], if it doesn't, does the error still point to the ini_set line? [assuming from your above comment that's where the current error points]

*I remember reading once [a while ago I admit] that a file being UTF-8 might screw up sessions. Trying to find a link

Ok I found someone who submitted a bug report concerning UTF-8 and session_start. Apparantly it isn't a bug - I didn't look into why - but either way it isn't quite the same issue. A type of UTF-8 encoding does cause session errors, just not the cookie error you are getting. See here if you are interested - UTF-8 Error

Etzeitet
  • 1,865
  • 2
  • 18
  • 22
  • I hope it isn't UTF related - the whole reason for starting this project is to internationalize and improve what went before. – graham.reeds Jul 14 '09 at 05:44
  • That works. However doesn't really help separate the components out. That said config and session will be used on every page immeadiately so it is a trade off I can live with. How do I check page type in Eclipse? – graham.reeds Jul 14 '09 at 05:59
  • Found that. All set to Cp1252. Also it appears that a blank (or rather a file that has code that is commented out) php file will cause the error with session_start. – graham.reeds Jul 14 '09 at 06:02
  • This lead to my workaround: It seems if you have index.php which includes config.php, and inside config.php include session.php it works. – graham.reeds Jul 19 '09 at 12:57
  • Glad you found a workaround, graham. It is rather a perculiar error though, be interesting to learn what it was. Although the important thing is you have it working :) – Etzeitet Jul 19 '09 at 15:48