I am new to PHP and am trying to build a facebook application. In the index.php file, I do the following :
if(!isset($_SESSION)){
session_start();
}
the facebook sdk seems to start the session by itself, so my code, doesn’t get called at all ! meanwhile, index.php has a form that gets submitted to a new file called processForms.php. In index.php, i set the following:
$_SESSION['uid'] = $me['id'];
But in processForms.php, accessing $_SESSION gives me the following error:
Notice: Undefined variable: _SESSION
Not sure whether I am getting the whole concept of sessions wrong here.
You don’t need an
ifbeforesession_start. Just call it straight:session_start();. If the session has already been started, it will do no harm.It seems you get the concept of “starting the session” wrong. Well, not exactly wrong… Look: “session_start()” does not “starts the session”, in the sense you may be thinking, i.e., you call it once and the “session has been ‘started'”. No. You need to call “session_start” always, in the beginning of every PHP script which will deal with sessions.
For example, at
login.php:At
somepage.php:You need the “session_start” call there. It does not mean “the beginning of the session”. It means “initialize the session stuff, so I can use sessions”.
An usual approach is to have a single
session_startin a “bootstrap” kind of script, and include it in the beginning of all other scripts.“bootstrap.php“
“a_page.php“
“another_page.php“