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.
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.
Session variables are set with the PHP global variable: $_SESSION.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With