Connor Bishop Connor Bishop - 1 year ago 41
HTML Question

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

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.

Answer Source

While conditions in View are perfectly fine, your View should not be fetching data from any source by itself and it should only work on data your Controller (or Presenter, depending on whatever is your application architecture) feed it with. In this case your Controller should check $_SESSION and make the decision what mode your View should display and pass that decision to your View for execution (i.e. action_mode = edit|add), but your View shouldn't not know why it switches between edit and add modes. It only must know how to do that.

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