Is it better to access data from an class object or from $_SESSION in PHP?

chriszo111

I am coding a sample object-oriented chat system using AJAX, SQL, PHP to train myself. I am saving "sensitive" user data into a database to check for a variety of inputs and processes such as login, online status, messages sent etc.

However, when a user logs into the system I check if $_SESSION['status'] is true or false to grant access to different locations of the website. I check the data in a separated file with a class Login.class and from there I could now decide whether I keep the object of Login.class to receive information of the user and use it, or I "kill" the object after I saved everything in $_SESSION variables (i.e. $_SESSION['username'] and so on…).

Basically I am interested in what is more secure or good programming and what is not. I know that php-login project is using both ways. It's kind of telling me that both of them are ok because a lot of professionals have been implementing this code. I thought that you should stick to one method, or am I misunderstanding something?

EJTH

Aside from the initialization of the actual session stuff (You already know that, with session_start() which should probably go in your bootstrap somewhere), you should ideally attempt to NOT use $_SESSION directly, why you ask?

Because of maintainability! Say you wanted to implement another form of session, maybe you want to use memcache instead of flat file sessions, use cookies only (I say only because PHP defaults to store your session ID in a cookie, so you are in fact already using cookies!)

Also ease of reading, and minimal chance of a typo going unnoticed and causing a bug (Maybe you have to check for admin state alot, and you mistype it $_SESSION['is_amdin], now had you used OOP here (Eg. Auth::isAdmin()) you would have recieved a fatal error about a missing function)

If you try to keep your stuff OO and use a proper IDE, you will also recieve the added benefit of autocompletion and code hints, you can PHPDOC a function, which will be used by many IDE's to provide documentation popups as you write. An assoc array like $_SESSION does not have any way to define hints for editors or humans!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ember: Access ember data 'store' object from utility class

From Dev

Ember: Access ember data 'store' object from utility class

From Dev

Access an object from an other class

From Dev

Access data in datagridview from a class

From Dev

Access data from another class

From Dev

How to access Session data from database

From Dev

include object inside a class or better derive from that class?

From Dev

Access Data from any object

From Dev

Access base class object from derived class

From Dev

Better/correct way to access included static information from inside of a class

From Dev

PHP Session getting data from database

From Dev

extracting data from php SESSION var

From Dev

Php MySQL Data from a class

From Dev

Access to Configuration object from Startup class

From Dev

How to access class module from object instance?

From Dev

Access HttpContextBase object from static class

From Dev

Access object from another class c++

From Dev

Access object in main from other class

From Dev

How to access an object from another class

From Dev

Access an Object in Another Class from an Interface?

From Dev

How to access class module from object instance?

From Dev

Access an object from another class in private method

From Dev

Flask - Access request object from method in class

From Dev

extracting data from Object in PHP

From Dev

Pull data from a PHP object

From Dev

Access Session values from static properties of static class

From Dev

Access saved model session object from controller into view

From Dev

php - Access outer class from an anonymous callback

From Dev

PHP access methods from parent class

Related Related

HotTag

Archive