11

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.

Chip Thrasher
  • 332
  • 2
  • 4
  • 15
Krimson
  • 7,386
  • 11
  • 60
  • 97

7 Answers7

8

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.

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889
2

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.

davidethell
  • 11,708
  • 6
  • 43
  • 63
1

Because of the manual session_start()

session_start — Start new or resume existing session

the same way you would connect to database every time you want to use it. it will connect to however you're storing your sessions. The session variables are no wiped out.

Also read more here but this should help to understand how sessions work:

When you are working with an application, you open it, do some changes and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn't maintain state.

A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping items, etc). However, session information is temporary and will be deleted after the user has left the website. If you need a permanent storage you may want to store the data in a database.

Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID. The UID is either stored in a cookie or is propagated in the URL.

Kasia Gogolek
  • 3,374
  • 4
  • 33
  • 50
1

Session data is stored at the Server side but the reference or id to the session is stored on the client's browser cookie. For the server to know your session id we make a call to session_start() on each page it is required (at the top) so that the first thing done is to get the id from the user and retrieve the session data. It is required on every page whenever you want to access session data.

Here is a video tutorial also. http://blip.tv/step4wd/php-sessions_en-5983086

M. Ahmad Zafar
  • 4,881
  • 4
  • 32
  • 44
0

No: it is NOT always going to create a new session. It only tells the script that this page wants to start OR maintain an existing session.

A session is nothing more that a STATE AT THE SERVER that you carry from from page to page. It is NOT accessible from the client (browser). The only thing the browser must do to keep the session is passing an ID (called default PHPSESSID in PHP).

This ID can be stored in a cookie, GET or POST, as long as you get it transfered to the server with each request you make.

Erwin Moller
  • 2,375
  • 14
  • 22
0

The answer is yes. You have to do that on every page. If you don't do that you get a undefined index error.

This will work because we include the file

Index.php

<?php
 session_start();

//file doesn't have session_start 
include "file.php";
?>
Jordi Kroon
  • 2,607
  • 3
  • 31
  • 55
-2

Youve to use session_start(), everywhere you need to work with session like, creating, accessing, destroying.

Unlike cookies, you can't access or work with session unless you initiate the session.

WatsMyName
  • 4,240
  • 5
  • 42
  • 73