Testing with session |
Hello,
I'm trying to test an "Auth" library I created. Obviously, that library uses the session. My first test looks like this: PHP Code: public function testConstructor() See the line where I ask if $_SESSION['user'] does not exist? That line produces an error: Code: ErrorException: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time If I understand it correctly, that means "session_start()" hasn't been called before the first "echo". I see two possibilities:
Thanks in advance!
SteeveDroz
> Which is the correct solution? What are the best practices? and how do I do that? Check out the PHP Online Manual for solutions. A simple test: PHP Code: if( isset($_SESSION) ) :
Just to be clear: I'm not a newbie with basic questions (I actually teach PHP and answer those basic questions all day long).
So first, your code should be: PHP Code: if (!isset($_SESSION)) Second, according to the PHP manual, session_start() "Start new or resume existing session", which means I wouldn't even need the if as calling that function is idempotent. Third, thank you for telling me I should use session_start(), but as you may have read in my question, I am in the testing context, within CodeIgniter. I very well know how to start a session, my question is where? In what class? What method? Maybe in a constructor? Or as a protected attribute? Do I change something in .env? Thank you for reading the question properly before answering.
(10-27-2019, 10:31 PM)SteeveDroz Wrote: Just to be clear: I'm not a newbie with basic questions (I actually teach PHP and answer those basic questions all day long).Ok, point taken, if I am going to use sessions I would put session_start(); near the start of index.php.
CodeIgniter ships with a MockSession class you can use. For an example of using it, check out my Myth:Auth package.
(10-28-2019, 06:34 AM)kilishan Wrote: CodeIgniter ships with a MockSession class you can use. For an example of using it, check out my Myth:Auth package. OK, it seems undocumented, but now I know it exists, I'll figure it out. Thanks a lot, that helps! Your example is also a great guide. |
Welcome Guest, Not a member yet? Register Sign In |