r/PHPhelp • • Nov 14 '15

Should I avoid session variables?

So I've been learning PHP in school, and our teacher really, really likes session variables. However, I have a web developer friend who says you shouldn't use session variables because...

As a rule of thumb, storing things in the session is discouraged, because it does not scale well (the server runs out of session storage). Ideally your design should be stateless on the server. i.e. no session data.

Since I'm really new at this, do any professional developers on here have a stance on this issue?

8 Upvotes

11 comments sorted by

2

u/f16falcon4 Nov 14 '15

I'm not a professional PHP developer so take this as you will, but my thought is the answer probably depends on the application. How large is the application? How often do you have to store variables at the session level? Are session variables even necessary for what you are trying to accomplish? Is this something that is going to be used frequently by many people, or is it something that you or your team might fire up from time to time?

Again, not a professional PHP dev. I've done some stuff at work for the team I'm on so I have some PHP experience, but I wouldn't count myself at the professional level.

EDIT: Added a question

1

u/[deleted] Nov 14 '15

I'm glad you responded even though you're not yet a "pro" :) thanks for the thoughtful response.

3

u/mgkimsal Nov 15 '15

i'm flummoxed as to how someone would suggest building anything non-trivial without the use of session data at the server end.

Can you give examples of what your teacher is storing in session variables? Storing objects of any sort is, imo, not a great practice, because you're having to reinstantiate those objects on each request, whether or not you're using them for that particular request.

State needs to be dealt with someplace. Having that state stored and managed on the client feels like a security nightmare waiting to happen.

Does your friend have any specific concrete examples of what he's advocating?

2

u/judgej2 Nov 14 '15 edited Nov 15 '15

No, don't avoid them completely - they should be used where needed. If some data needs to be a part of the session, i.e. stored and accessible against that user browsing session, then that data should be put into the session, and that usually means in session variables.

Data that does not need to go into the session - perhaps more permanent data - should not go into the session.

Like most strict "rules" - they are never black-or-white. There is always some subtlety that people who give you the rules often overlook or don't know.

Step 1: understand what a session is and why you would use it.

2

u/[deleted] Nov 15 '15

You can use alternative session stores that eliminate storage concerns. Food for thought.

1

u/Turbo-Lover Nov 15 '15

This would have been a really great answer if you had given some examples of alternative session stores. I think you're talking about something like redis, which is what I was going to recommend looking into as well. I guess I didn't realize there were competitors to redis out there.

1

u/[deleted] Nov 15 '15

Basically any database, SQL or not. It's not so hard to implement the built - in SessionHandlerInterface

1

u/[deleted] Nov 14 '15

I'd agree with your friend but make sure to acknowledge the first part of it, it's a rule of thumb. Whether or not sessions are appropriate completely depends on the problem you are trying to solve but if you do decide to use them, it's something that you should probably think about fully and understand the potential problems and limitations you might face.

What is your teacher using sessions for?

1

u/Hansaplast Nov 14 '15 edited Nov 14 '15

It really depends on your application, but session variables are the easiest way to share a state between different requests. If your application doesn't need to share data between requests, you shouldn't nee to use sessions in your application. The most common use for sessions is when a user logs into your website, you wouldn't want the user to specify their credentials every time they request a new page. Sessions are really fine in this context. Another example is to protect your user submitted data with csrf tokens, you'll need some kind of session to pass the token between requests. That's when sessions are handy. When you create an API it's better to have a stateless application. But in general the credentials are passed with every request (by for example JWT, Oauth etc.) and some kind of state is rebuild at every request. While it is possible to build a 'normal' website with these stateless authentication methods, it's kinda useless. I do use it sometimes, for example on a backend system where a JavaScript based application communicates with an API, but it is almost always more work.

Edit: if your server runs out of session storage you're doing something wrong. Besides this, there are multiple ways to store session data, for example in a database.

1

u/rifts Nov 15 '15

Sessions are great, if you decide to create anything beyond basic you will need sessions. For example if you create a login for your website and don't want users to have to login every time the page is refreshed you can simply use sessions.

1

u/liquid_at Nov 15 '15

both are kinda true. Session isn't a default-storage-place for everything you want to store. But It has its uses.

Don't store any large things in sessions, that's what localstorage is for.