Reputation: 117
I have learned so much from stackoverflow. I have run into a problem that is strange, Here it goes.
I have coding that checks if a session has started and if not, I start the session. No problem there
Here is my code
If (session-id = "")
{Session_start();
/* setting session ids */
}
I have an include file that submits a form to the same script (script above)
But when returning to the page via post method, the session gets started again, as if the session stopped.
Edit
If (session_id == "")
The mistake there was a typo.
The problem I originally had was that upon first initialization, I had set session arrays
/* If statement */
$_Session['test'] = ['1','2','3','4','5'];
/* end of if statement */
When user submits data to the same script, the session array will be null, giving an error. The way I solved it was to put session_start on the top of the script and no longer on the if statement. That seemed to solve the problem, thanks for all your input.
Upvotes: 1
Views: 257
Reputation: 57322
i think you want this
if(session_id() == '') {
Session_start();
/* setting session ids */
}
session_id()
returns the session id for the current session or the empty string ("") if there is no current session (no current session id exists).
also you Call session_start()
more than one there is no problem in that since
session_start()
- Start new or resume existing session
in the php version >=5.4.0
you can also use session_status()
it
session_status()
is used to return the current session status.
Edited
Well it seems that Starting session twice is problem in some case
I am quoting this comment of DaveRandom
If The session is already started. You can't start it again. You can't resume something that isn't stopped. This will stop the message from appearing but I don't understand why you would want to call
session_start()
in the first place. Note also thatsession_write_close()
doesn't destroy the local $_SESSION variable so make sure you don't try and write to it after you've closed the session.
so you can do this like
<?php
var_dump(isset($_SESSION));
session_start();
var_dump(isset($_SESSION));
session_write_close();
var_dump(isset($_SESSION));
session_start();
Upvotes: 4
Reputation: 57670
Check whether $_SESSION
is set.
isset($_SESSION) or session_start();
http://codepad.viper-7.com/I3i9lv
Upvotes: 1
Reputation: 2075
You need to start the session at the top of every page, just to keep the session. That will not clear the running session, so you can just remove the condition for it to start.
Upvotes: 0