How does PHP's session_start() function work?

  • PHP
  • Thread starter Math Is Hard
  • Start date
  • Tags
    Php
In summary, the session_start() function in PHP is used to start a session and store variables in the $_SESSION superglobal. It is necessary to call this function at each page to stay in the same session, otherwise a new session will be created. This function will also load variables from an existing session into $_SESSION.
  • #1
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,652
37
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
  • #2
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:
 
  • #3
Thanks, CompuChip! :smile:
 

1. What is PHP?

PHP is a server-side scripting language used for creating dynamic web pages and web applications. It stands for Hypertext Preprocessor and is widely used for its flexibility and ease of use.

2. What are sessions in PHP?

Sessions in PHP are a way to store and access user-specific data across multiple pages of a website. This allows for a more personalized and interactive experience for the user.

3. How are sessions created and accessed in PHP?

Sessions in PHP are created using the session_start() function. This function must be called at the beginning of each page where session data will be used. To access session data, the $_SESSION superglobal array can be used.

4. What is the difference between cookies and sessions in PHP?

Cookies and sessions both store data on the client-side, but the main difference is that cookies are stored on the user's browser while sessions are stored on the server. Cookies have a limited storage capacity and can be manipulated by the user, while sessions are more secure and can hold larger amounts of data.

5. How long do sessions last in PHP?

Sessions in PHP last until they are destroyed or until the user closes their browser. By default, sessions will expire after a set period of inactivity, but this can be adjusted using the session.gc_maxlifetime setting in the PHP configuration file.

Similar threads

  • Programming and Computer Science
Replies
21
Views
5K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
7
Views
6K
  • Programming and Computer Science
Replies
4
Views
6K
  • Programming and Computer Science
Replies
7
Views
5K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
2
Views
5K
  • Programming and Computer Science
Replies
6
Views
6K
  • Programming and Computer Science
Replies
15
Views
3K
Back
Top