Max Koretskyi
Max Koretskyi

Reputation: 105499

session destructrion and renewal when session expires

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

Answers (4)

Maarkoize
Maarkoize

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:

  • the browser is closed
  • the session is unset/destroyed
  • the variable is unset

To access $_SESSION[] variables there must be a session_start() at the top of the page, which should access a $_SESSION[] variable.

Upvotes: 1

Makrand
Makrand

Reputation: 587

Better you do this :

if (session_id() == "") {
  session_start();
}

& your value won't be altered after another hit otherwise too

Upvotes: 0

Nihat
Nihat

Reputation: 3115

Session_start() resumes an existing session if one is started before. So your code is just fine

Upvotes: 1

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

Related Questions