How does PHP's session_start() function work?

  • Context: PHP 
  • Thread starter Thread starter Math Is Hard
  • Start date Start date
  • Tags Tags
    Php
Click For Summary
SUMMARY

The PHP function session_start() is essential for managing user sessions in web applications. Each time a page is accessed, session_start() must be called to either initiate a new session or resume an existing one. If a session already exists, this function loads the session variables into the $_SESSION superglobal, allowing access to stored data. Failure to call session_start() results in the creation of a new session, leading to the loss of previously stored session data.

PREREQUISITES
  • Understanding of PHP programming language
  • Familiarity with web session management concepts
  • Knowledge of superglobals in PHP, particularly $_SESSION
  • Basic understanding of cookies and how they relate to sessions
NEXT STEPS
  • Explore PHP session management best practices
  • Learn about session security measures in PHP
  • Investigate the use of session_destroy() for terminating sessions
  • Study how to implement session handling in frameworks like Laravel
USEFUL FOR

Web developers, particularly those working with PHP, backend developers managing user sessions, and anyone interested in enhancing session management in their applications.

Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,663
Reaction score
36
I'm a little confused about how PHP's session_start() function works.
There's a tutorial I am reading here: http://www.tizag.com/phpT/phpsessions.php

Here's an example they give of a counter script:

Code:
<?php
session_start();  
if(isset($_SESSION['views']))
    $_SESSION['views'] = $_SESSION['views']+ 1;
else
    $_SESSION['views'] = 1;

echo "views = ". $_SESSION['views']; 
?>

It seems like every time I call this PHP script from a new page, a new session will start, because the first line is:

session_start();

Does PHP ignore this function if a session exists?

Thanks.
 
Technology news on Phys.org
You have to call session_start() at each page. If no session is started yet, this will start a new session (i.e. write a cookie with the session ID). I believe that if you already have a session running, this will load the variables into $_SESSION so you can access them.

So basically, session_start() is needed if you want to stay in the same session, if you don't call it you will get a new session all the time (or your variables just won't get stored, which is effectively the same).

In short: I Don't know how it works exactly, just use it :smile:
 
Thanks, CompuChip! :smile:
 

Similar threads

  • · Replies 21 ·
Replies
21
Views
6K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 22 ·
Replies
22
Views
2K