n1concepts Posted January 16, 2012 Share Posted January 16, 2012 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 More sharing options...
blacknight Posted January 17, 2012 Share Posted January 17, 2012 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 https://forums.phpfreaks.com/topic/255186-best-method-to-define-as-reusable-code/#findComment-1308394 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.