Jump to content

Best Method to define as reusable code


n1concepts

Recommended Posts

Hi,

 

I just want to get afew opinions on the best way to define this code which will (literately) be the same on multiple pages.

Note: objective is to check for an existing set of $_SESSION variables and redirect accordingly (will be placed at top of each page).

My question is, "can't this be define as a function - passing in the required values?" and if so, "are there any issues managing secured logins in this manner?

 

Here's the snippet of code for review - not as a function:

 

if (($_SESSION['uname']) && ($_SESSION['access'] == 2)) {
   #if already logged in, redirect based on dept id
    if ($_SESSION['dept'] == 1) {
   header("Location: admin/index.php");
   } elseif ($_SESSION['dept'] != 1) {
   header("Location: cases/cases-list.php");
} else {
// If access is not GRANTED then reset and hold user at login page
     unset($_SESSION['uname']);
     unset($_SESSION['access']);
     header("Location: index.php");
}
}

 

So, my question is "what is the correct method of defining this as a function - passing in three variables which would all be sessions 'uname', 'access', 'dept'?

 

thx!

Link to comment
https://forums.phpfreaks.com/topic/255186-best-method-to-define-as-reusable-code/
Share on other sites

function Auth()
{
if (($_SESSION['uname']) && ($_SESSION['access'] == 2)) {
#if already logged in, redirect based on dept id
if ($_SESSION['dept'] == 1) {
header("Location: admin/index.php");
} elseif ($_SESSION['dept'] != 1) {
header("Location: cases/cases-list.php"); } 
else {
// If access is not GRANTED then reset and hold user at login page
  unset($_SESSION['uname']);
  unset($_SESSION['access']);
  header("Location: index.php"); }
}
}

 

then call Auth(); at the top of all your pages

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.