Reputation: 105499
I put the data into session:
session_start();
$_SESSION['color']='green';
Next time when I receive query the session expires. What will happen? Will old session data be destroyed and new session created with session_start()
? Will I be able to access data stored from previos requests?
Upvotes: 0
Views: 84
Reputation: 2621
session_start()
starts a new session or uses an existing one.
So if the session is not destroyed before $_SESSION[]
variables will still be there.
$_SESSION['color']
has the value 'green' until:
To access $_SESSION[]
variables there must be a session_start()
at the top of the page, which should access a $_SESSION[]
variable.
Upvotes: 1
Reputation: 587
Better you do this :
if (session_id() == "") {
session_start();
}
& your value won't be altered after another hit otherwise too
Upvotes: 0
Reputation: 3115
Session_start() resumes an existing session if one is started before. So your code is just fine
Upvotes: 1
Reputation: 68476
What will happen? Will old session data be destroyed and new session created with session_start()?
Until the browser is closed or you implicitly call session_destroy();
, the session variable will still exist and you can access them on any page.
Will I be able to access data stored from previos requests?
Yes you can. Just add session_start();
on the top of your PHP code and you could access it like echo $_SESSION['color'];
Upvotes: 1