Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

<?php 
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.

like image 715
Connor Bishop Avatar asked Jul 05 '16 06:07

Connor Bishop


People also ask

What is the purpose of using session variables?

Session variables are special variables that exist only while the user's session with your application is active. Session variables are specific to each visitor to your site. They are used to store user-specific information that needs to be accessed by multiple pages in a web application.

Which variable is used to access the session variables?

Session variables are set with the PHP global variable: $_SESSION.

Can you view session variables?

No. Session variables are stored on the server. The only thing that would be visible in Firefox is the ID of the session, stored in the session cookie (e.g. PHP_SESS_ID=randomgarbage ). Save this answer.

What are session variables and its type?

About a session variable A session variable value is a user-defined variable. A variable stores data of a specific data type. A session variable is immutable within the variable scope of a user session.


1 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.

like image 184
Marcin Orlowski Avatar answered Sep 19 '22 10:09

Marcin Orlowski