Search code examples
phpmysqluser-roles

How not to make a db call for checking user role in php?


I know the standard way to check user roles mostly like if(isUserAuthorizedForTheOperation(userId)) in PHP which is create a db and for every user check the db and find out.Now this adds to immense performace issues.For every hit in my .php file,I find it extremly costly to query the database.What is the alternative?


Solution

  • Do you perform one query per hit/session or multiple? Make sure you only load in the roles once, store it in a (session) variable, and then keep checking that variable - instead of performing a query for every time you want to check.

    (Taken from the comments as OP said it answered his question.)