Store session, file , database, memory, which better

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
coolesting
Forum Newbie
Posts: 13
Joined: Thu Sep 02, 2010 12:36 am

Store session, file , database, memory, which better

Post by coolesting »

Hi, guys,
as the topic as i said, which better to save the session data, default method in php is file storage .
what do you think about this question.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Store session, file , database, memory, which better

Post by alex.barylski »

There is no "better" only different. DB hosting is probably more secure and robust and scalable if configured properly. Files are more risky on shared servers, but require no additional implementation or library.

Cheers,
Alex
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Store session, file , database, memory, which better

Post by AbraCadaver »

Also, suppose you want to display how many users are online or the usernames of the users online, etc. I don't know how you can achieve this with the file based sessions, but with a DB it is just a simple query.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Store session, file , database, memory, which better

Post by social_experiment »

AbraCadaver wrote:I don't know how you can achieve this with the file based sessions, but with a DB it is just a simple query.
Out of curiosity: wouldn't an SQLite file-based database be able to do this? I think if a choice has to be made, go with databases, in the long run it will probably save you a lot of hassles.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Store session, file , database, memory, which better

Post by VladSun »

AbraCadaver wrote:Also, suppose you want to display how many users are online or the usernames of the users online, etc. I don't know how you can achieve this with the file based sessions, but with a DB it is just a simple query.
:D
viewtopic.php?f=1&t=82659&p=460330#p460330
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Store session, file , database, memory, which better

Post by VladSun »

alex.barylski wrote:Files are more risky on shared servers
One just needs to set the session files save path (if it isn't already set by the hosting company) to a path in his/her own directory in order to get fulll privacy.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Store session, file , database, memory, which better

Post by superdezign »

But the database is still more powerful since you have the ability to precisely control the death of those sessions. PHP holds onto session data past the expiration and is as limited as the GC.

Database is my personal preference, but it is also the slowest. Memory is the fastest to access, though it requires a bit of extra logic to set up and, AFAIK, has difficulty working on shared servers. The reason that the file method is the default is because it's a stable medium.
Post Reply