Before I post my problem I would like you in advance for taking the time to read this.

I've been using simpletest to help me develop and test my php scripts.

I'm currently working on a session class and when I go to test it I get the following exception from simple test.

Unexpected PHP error [session_start(): Cannot send session cache limiter - headers already sent]

I have checked all my php files for leading white space. I have checked all the classes that I'm using to see if they write to the browser. They don't. They are just classes, as in controllers and don't write to the browser. I have also checked my files for BOMs [Byte Order Markers], but there are none and the program that I use saves all files without a BOM.

The problem is simpletest and the session class that I'm trying to test. Simple test starts a new session in order to test my class, but I need to test whether a session is started and destroyed in my session class. In other words, the two are fighting.

Are there any ways around this? I have tried using

ob_start();

and corresponding functions with no luck.

Thanks for your time.

Dodzi

Recommended Answers

All 4 Replies

move that ob_start(); to the 1st line before the doctypes

or try changing your html encoding to utf8 without BOM

or try changing your html encoding to utf8 without BOM

vaultdweller,

Thanks for your reply. Like I stated above. I've already tried using the steps that you gave as a possible solution. I actually know what the problem is, I just don't know how to solve it...

The testing framework simpletest displays test results in an html format. Because of that it initiates session_start... I'm trying to test my own session controller inside of that... because of that I will ALWAYS get the session started error. The only way I know around it is to test my session controller without using simpletest... but that would defeat the purpose of simple test...

Any ideas? Do you have any experience with this doing simple test?

nexocentric

After lots of searching and trial and error I have found a solution to the problem. I don't know if this is a true solution or not, but I'm posting what worked for me so that someone else doesn't have to go through it again.

1) When working with simpletest and testing things that deal with sessions and/or cookies, extend your test class with simpletest's WebTestCase. By doing this you get access to simpletest's WebTestCase->restart() function. This resets the browser to the state it would have been in BEFORE it started loading a page.

2) Go head and call this function in the setUp section of your test cast. simpletest test cases have a setUp() function that you can use to initialize variables as well as do any other setup that you might need. Call WebTestCase->restart() in the setup function before doing ANYTHING that that requires $_SESSION access.

3) Prefix the php function session_start() with an '@' symbol. By doing this you tell the php server that any warnings that come from calling this function are to be ignored.

This worked for me and I'm having no problems at the moment.

Thanks for your input.

Dodzi

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.