Mohebifar
Mohebifar

Reputation: 3411

FileCache vs Session in php

Session or FileCache ? Which one is better to use ?

For example when a user logged in, I want to save some data like username, password, id, details, etc. as long as he didn't logout.

I can save this data serialized in some file. and also I can save it in session.

What should I do ?

Upvotes: 2

Views: 158

Answers (3)

Hadi Mostafapour
Hadi Mostafapour

Reputation: 2266

File or session cache are so similar, because sessions also written in files, but in fact session are more practical and easier to use, my prefer is Memory Cache, Like Mysql Memory Engine or APCU. just try once.

Upvotes: 1

Steven Moseley
Steven Moseley

Reputation: 16325

The best performing solution would be to use the Session, setting your session save_handler to memcached.

First, install memcached

apt-get install memcached
apt-get install php5-memcache

Then, edit your php.ini to write to memcached instead of file

session.save_handler = memcache
session.save_path    = "tcp://localhost:11211"

Finally

sudo /etc/init.d/apache restart

Then, when you read and write to/from $_SESSION it will be using Memcached

Upvotes: 1

Andy  Gee
Andy Gee

Reputation: 3315

Save it in a session. It's stored in memory, faster and generally more secure than a file. Usually you wouldn't store a password in a session as it will be removed once the user leaves the website. Passwords are usually stored in a database sometimes in files but passwords should always be encrypted. Use md5($password.$email) or something similar. The json data format makes this quite simple.

$user['temp_password'] = md5($user['password'].$user['email']);
$user['id'] = 45;
file_put_contents('user_settings.json',json_encode($user));

Upvotes: 2

Related Questions