Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create PHP session for all users or only ones who want to log in?

Tags:

php

session

At the moment my CMS creates a PHP session for every user who visits the site. I'm thinking about only creating a session for users who want to log in. The problem I have here is that some UI elements for logged in users are on all pages, so on every page request the system has to check if the user is logged in, which means I have no other option to start a session on every page request? Or am I wrong? Is it normal practise to create a session for every user, even if dose not want to log in?

Short, I'd like to know if A. there is an option in my use case to only create a PHP session for users who want to log in and B. if it consider bad practise creating a session for every user, regardless if he wants to log in or not. If this isn't the case, I can leave things as they are really ...

like image 438
wowpatrick Avatar asked Jan 13 '11 22:01

wowpatrick


2 Answers

You have no (real) choice. You can not know, that a user is logged in (or not) without a session.

like image 163
KingCrunch Avatar answered Sep 30 '22 11:09

KingCrunch


Quick answer:

In your use case, it is perfectly fine to create a session for every user. Sessions are negligible and not something to worry about as far as performance goes (in your case).

The method you're using is not bad practice at all. In fact I'd say it's pretty near best practice.

Long answer: In my 6+ years of experience as a PHP programmer in the corporate world, it is perfectly normal to create a session for every user, regardless of whether or not they're logged in. In fact, sessions can be used to do a lot of convenient features for a user even when they're not logged in -- such as shopping carts, etc. You're doing things right. If you want to speed up performance at all, use a tool like Google's Pagespeed and Yahoo's YSlow -- they'll give you tips on best practice for websites.

like image 24
GarrettNow Avatar answered Sep 30 '22 12:09

GarrettNow