/    Sign up×
Community /Pin to ProfileBookmark

How many variables can be registered in one session?

Does anybody know how many variables can be registered in one session? I can’t find in the PHP Manual where it tells how many variables a session can hold.

Here’s what the Manual says at the beginning:
[url]http://www.php.net/manual/en/ref.session.php[/url]

[quote]

The session support allows you to [B]register arbitrary numbers[/B] of variables to be preserved across requests. When a visitor accesses your site, PHP will check automatically (if session.auto_start is set to 1) or on your request (explicitly through session_start() or implicitly through session_register()) whether a specific session id has been sent with the request. If this is the case, the prior saved environment is recreated.

[/quote]

I combed through the Manual the other day but could not find any information that tells about any limitations on variables that can be contained within a session.

I would appreciate any help.

Thanks.

to post a comment
PHP

21 Comments(s)

Copy linkTweet thisAlerts:
@scragarOct 13.2007 — I don't think you'll run into much trouble, before now I've stored a huge array to sessions(around 100 items in the array, and about 5 other things stored alongside it).

I'm going to guess that it's an option that can be configured though, otherwise it's kinda weird not to mention the specifics(unless they fear that it would cause confusion)...
Copy linkTweet thisAlerts:
@ZnupiOct 13.2007 — Since sessions are basically files on your hard disk, you can store as many variables as much free disk space you have. Usually, you won't need more than that.
Copy linkTweet thisAlerts:
@felgallOct 13.2007 — If you can't store the number of variables you need then you just need to get a bigger hard drive to store them on. Of course the page will be so slow as to be unusable long before you can fill even a small hard drive with the session variables.
Copy linkTweet thisAlerts:
@KnoxVolauthorOct 14.2007 — Thanks everyone for responding to my problem. I appreciate the help.
Copy linkTweet thisAlerts:
@bokehOct 15.2007 — Since sessions are basically files on your hard disk, you can store as many variables as much free disk space you have.[/QUOTE]That is bogus. The limiting factor is the maximum RAM allocation in php.ini, 128 megabytes by default in the latest version, but don't forget variables consume much more RAM than their physical size in a file. For example, I have a script which has an array with 50,000 entries, each one holding an integer between 1-7000. When the script runs that array consumes 8 megabytes of RAM even though the source file is only a few hundred kilobyes in size.
Copy linkTweet thisAlerts:
@NightShift58Oct 15.2007 — That is bogus. [/QUOTE]
I second that.

I have yet to see a server with more memory than drive space. While it's true that session files have to fit on the drive, they are useless unless they can be loaded - as needed - into memory.

The overall limiting factor will therefore be the size of the session array (as opposed to file size) AND the number of concurrent sessioned visitors.
Copy linkTweet thisAlerts:
@ZnupiOct 15.2007 — You can always use ini_set("memory_limit", "-1");.
Copy linkTweet thisAlerts:
@NightShift58Oct 15.2007 — You can always use ini_set("memory_limit", "-1");.[/QUOTE]

It doesn't really matter how far I want to drive... the gas needs to fit in my tank...
Copy linkTweet thisAlerts:
@ZnupiOct 15.2007 — You can always take some gas bottles with you and put them in your trunk ?
Copy linkTweet thisAlerts:
@NightShift58Oct 15.2007 — Yes... and enough of those need to fit in the car as well...
Copy linkTweet thisAlerts:
@MrCoderOct 15.2007 — I second that.

I have yet to see a server with more memory than drive space. While it's true that session files have to fit on the drive, they are useless unless they can be loaded - as needed - into memory.

The overall limiting factor will therefore be the size of the session array (as opposed to file size) AND the number of concurrent sessioned visitors.[/QUOTE]


Nvidia have a computer with 768gig of ram :/
Copy linkTweet thisAlerts:
@felgallOct 15.2007 — You are assuming that every page needs to access every session variable. In that case the web page size would be the limiting factor and not the RAM since no one will wait for even a 1Mb page to download and so that can't possibly hold all the session fields in a meaningful way.

If each page only references 0.00001% of the session variables and you configure things so that only the session variables that are needed get read then you can have a hard drive 100000 times the size of your RAM and still have the hard drive rather than the RAM as the limiting factor.

Since the page size will limit the session variables that each page can reference, unless you have millions of visitors at the same time the hard drive space will be more restrictive than the RAM if you have huge numbers of session variables.
Copy linkTweet thisAlerts:
@pcthugOct 16.2007 — I don't roll with standard PHP sessions, mainly for scalability issues, so I might be wrong.


If each page only references 0.00001% of the session variables and you configure things so that only the session variables that are needed get read then you can have a hard drive 100000 times the size of your RAM and still have the hard drive rather than the RAM as the limiting factor.[/QUOTE]

So correct me if I am wrong, but aren't each clients session variables stored in a separate file, the variables being stored in there serialized form? We can call this the session data file.

Then on each page load (if the client presents a valid/existsing PHP Session Id) when the session is started, the relative session data file is loaded, unserialized and all variables are used to populate the $_SESSION superglobal.

So it wouldn't matter if you only wanted to use even 1 of the 5000 variables in the session data file, all 5000 would be loaded, unserialized and used to populate the $_SESSION superglobal.
Copy linkTweet thisAlerts:
@felgallOct 16.2007 — You can override the way that the session data is stored. If you set it up to store each field separately in a database then you can have it only retrieve the fields that the page actually uses. It is all a matter of how you code the replacement modules for handling sessions - which you would want to do if you have more than a couple of dozen variables in any case.
Copy linkTweet thisAlerts:
@NightShift58Oct 16.2007 — You are assuming that every page needs to access every session variable. In that case the web page size would be the limiting factor and not the RAM since no one will wait for even a 1Mb page to download and so that can't possibly hold all the session fields in a meaningful way.[/QUOTE]I think there's a misconception here: session variables, if any, are loaded into memory, server-side, and not in a page sent to the client. As such, the 1Mb you're referring doesn't go anywhere but stays in server memory for a while.

Coding your own session handler is an option - but I stay away from this and other extreme sports. There would be much easier ways to deal with it. Going the database route would likely be counterproductive, at least in terms of drive space (table + index) and performance is likely to take a hit as well.

And, in the end, memory will still be your limiting factor, given enough sessioned visitors.

Theory aside, I don't think that session variables are the proper vehicle for storing massive amounts of data. Perhaps that should be the answer to the OP: use sessions wisely.
Copy linkTweet thisAlerts:
@NightShift58Oct 16.2007 — Nvidia have a computer with 768gig of ram :/[/QUOTE]Birthday coming up shortly and the GOOD news is that FedEx, DHL and UPS all service Costa Rica.

Since you'll be paying in quids, it's only half price for you. Send two. The spare parts logistics isn't too hot around here...
Copy linkTweet thisAlerts:
@MrCoderOct 16.2007 — Birthday coming up shortly and the GOOD news is that FedEx, DHL and UPS all service Costa Rica.

Since you'll be paying in quids, it's only half price for you. Send two. The spare parts logistics isn't too hot around here...[/QUOTE]


What you getting me for my birthday?

Mine is very soon too ?
Copy linkTweet thisAlerts:
@bokehOct 16.2007 — Since you'll be paying in quids[/QUOTE]Not another Brit!
Copy linkTweet thisAlerts:
@bokehOct 16.2007 — all 5000 would be loaded, unserialized and used to populate the $_SESSION superglobal.[/QUOTE]That's correct. It would also be the same if the session were stored in the DB.
Copy linkTweet thisAlerts:
@NightShift58Oct 17.2007 — What you getting me for my birthday?

Mine is very soon too ?[/QUOTE]

By the time the Nvidias get here, I'll have thought of something. Trust me...
Copy linkTweet thisAlerts:
@MrCoderOct 17.2007 — Not another Brit![/QUOTE]

There is more then one?
×

Success!

Help @KnoxVol spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...