Home » Php » php – Is checking the value of SESSION variables classed as business logic?

php – Is checking the value of SESSION variables classed as business logic?

Posted by: admin July 12, 2020 Leave a comment


I’m developing my a web application.
I have this piece of code:

if($_SESSION['add'] == 1) 
echo '<input type="button" name="add" id="add" value="Add" onclick="add()" >';
if($_SESSION['edit'] == 1)
echo '<input type="button" name="edit" id="edit" value="Edit" onclick="edit()">';

Basically, when the user logs in, I set session variables which indicate whether or not that user is authorised to make changes and add records. So when they get to the home page, I use this code to decide whether or not to display my add and edit buttons.

Is this code breaking the rule of logic/presentation separation? If so, how can I achieve separation?

I am not using any web framework.

How to&Answers:

While conditions in View are perfectly fine, your View should not try to fetch any data from any source by itself. This is not its role and it should only work on data your Controller (or Presenter, depending on whatever your application architecture is) feed it with. Your View shouldn’t not know the logic behind why and when to switch between edit and add modes. It only must know how to do that when ordered. In your case, Controller should check $_SESSION and make the decision what mode, add or edit your View should display and pass that decision to your View (i.e. action_mode = edit|add) for dumb execution.

PS: I recommend to make a habit to always put code blocks (even one-liners) in {, } brackets.