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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.