I am using MVC and also PDO. I am trying to update a string depending on whether the session is empty or not, so in my index.phtml I have:
Move
session_start();
to the top of your file, it has to be the first thing executing in your file.
The session must be running before any writing or reading is done with the session variables or any output is written to the browser. What matters is that you don't want sessions to exist depending on whether they have a proper key with a proper value or not; if you may at some point in your code require a session value from a session key then you always want to have an ongoing session, i.e. by calling it from the beginning, so that it's available when you need it, without you having to understand what's breaking it.
It is also important to know that calling session_start()
in a.php
will not make the session live in b.php
unless session_start()
is called in b.php
aswell. Or, if you redirect a.php
to a.php
(refresh) then the session will not live unless you have session_start()
at the beginning of the file, so using if statements to determine whether a session should be ongoing is not going to work, values will be lost in between, if it works to begin with.