Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Session variables

I am beginning to learn php. I have a question regarding sessions.

Right now, I know that session_start() creates a session variable.

What I don't know is, when I access the session I created, do I need to use session_start() again?

If yes...

Why is this? Because I already created a session and I wonder why it wouldn't last the entire browsing session.

like image 895
Krimson Avatar asked Aug 06 '12 10:08

Krimson


2 Answers

because what i understand from it is, that it is going to create a new session.

No:

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

http://php.net/session_start

Each new page you visit is an entirely new context for PHP. session_start allows you to reestablish a previous context/session/data.

like image 73
deceze Avatar answered Sep 26 '22 17:09

deceze


The session_start function tells PHP to enable session tracking. It doesn't wipe out the session created by a previous page. You must call session_start() before you'll have access to any variables in $_SESSION.

like image 30
davidethell Avatar answered Sep 25 '22 17:09

davidethell