1

I have multiple session variables. How many times do I have to call session_start in a page? If only once, is it going to account for four session variables e.g. $_SESSION['a'],$_SESSION['login'],$_SESSION['b'],$_SESSION['c']?

good_daddy
  • 21
  • 3
  • `session_start` is just to enable the session system, you can add _a lot_ of keys to the global session array – JimL Jan 03 '16 at 14:09
  • yah, that means you only need to call session_start once in each page – Webster Jan 03 '16 at 14:11
  • I want to use these session variables. How many of them can I use, if I use session_start at the beginning of the page? – good_daddy Jan 03 '16 at 14:11
  • Okay, I get it. So, if I initiate various session variables throughout the page, I need to call session_start once. Right? – good_daddy Jan 03 '16 at 14:12
  • @good_daddy See http://stackoverflow.com/questions/4649907/maximum-size-of-a-php-session – Tomas K Jan 03 '16 at 14:13
  • I followed the link, so I think the answer to my last question in the comments is 'yes'? – good_daddy Jan 03 '16 at 14:16
  • @good_daddy Yes. As long as you start the session at the very top of your script so you don't get "Cannot modify header information - headers already sent (..)". You can store maximum 128 mb by default. – Tomas K Jan 03 '16 at 14:18
  • Thanks. So, I am putting it over – good_daddy Jan 03 '16 at 14:19
  • @good_daddy yep. put it at line 1 or so – Tomas K Jan 03 '16 at 14:21
  • @JimL - Comments are for discussion of the question and are considered second-class citizens on SO. As you have answered the question - if you post it as an answer, I will upvote. – random_user_name Jan 03 '16 at 14:24
  • @cale_b I know - normally I don't have time to write a proper answer so I just comment where I see I can provide some constructive info. This weekend I'm alone with the kids so SO time slots are rare and short :P – JimL Jan 03 '16 at 15:05

2 Answers2

0

You only need to call session_start once. It will account for all of your $_SESSION variables.

Within the session, you can have practically as many session variables as you like.

Best practice is to call session_start at the top of your script, before any output is generated.

random_user_name
  • 25,694
  • 7
  • 76
  • 115
0

To use the session you need to call session_start() before using $_SESSION variables. To avoid errors like

Cannot modify header information - headers already sent (..)

call session_start() on the very top of your script.

By default the maximum size of the session is the maximum memory of your script - 128mb. For further information this SO question.

Community
  • 1
  • 1
Tomas K
  • 361
  • 4
  • 17