Let's see the code:
Your
Session
class does too many things:- Session management
- Login
- Data validation
- Logout
- Authentication
- Inclusion management
Your class has no clean way to destroy a session.
You don't verify if a session is active.
Your constructor seems a little off. Sessions are a singleton. Use a single
static final
class.You don't need to repeat
public
on every method;You are storing a class on a
$_SESSION
. I would avoid that like plague. You should start and end the session. And you should have a method to save the session (that runssession_write_close()
on shutdown or at any given time).There's no way to check if a session was opened before or not, or if it was saved already or not.
Fixing these points and splitting your code into classes that do each thing in separate will make your code better in every conceivable way.
A while ago I've used JSON to store the session data. It generates files a bit longer, but it is faster to save. Consider making your own session management system, based out on PHP's way.
Since HDD is cheaper than CPU cycles, I would consider using such way to load/save your sessions.
Completely outside the scope of this review, you could use a RAMdisk with the /tmp
partition to squeeze every tiny piece of performance you get. Use only 10% of your RAM for such purpose.
Before doing anything to store your password, I recommend a good read:
How to securely hash passwords?How to securely hash passwords?
One of the points is that you shouldn't use SHA1, but instead use bcrypt
or password_verify
(PHP5.6 and up) in the future.
Also, you should be extremely careful with SQL injection and other possible attacks, especially evolving different encodings.
I sound paranoid, and I know that. But I just want a safe code.