jerry89 Posted February 19, 2009 Share Posted February 19, 2009 Hey guys, hope you can help me. I want to make a access level in my cms that I have made , its pretty basic, its my testing platfrom to develop my php skills. Its got 3 levels Admin , Client and Guest. I have look around google, but didn't find anything that will help me a lot. So far i know , i need to use "if/else" with redirect. so if client log in, it will redirect, to client.php same for quest, but not for admin. Well I hope some one can give me some directions. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/145918-user-access-level/ Share on other sites More sharing options...
Ayon Posted February 19, 2009 Share Posted February 19, 2009 some code would be helpfull here.. to see how you check if user is logged in etc... like i use sessions.. then i just do <?php if ($_SESSION['user']['user_level'] == 'admin') { redirect to admin page } ?> Quote Link to comment https://forums.phpfreaks.com/topic/145918-user-access-level/#findComment-766066 Share on other sites More sharing options...
jerry89 Posted February 19, 2009 Author Share Posted February 19, 2009 I have session and also i got function for check if logged in. so now i need is the "if/else" to redirect. Would that change anything on the php code you inlcude in? Quote Link to comment https://forums.phpfreaks.com/topic/145918-user-access-level/#findComment-766100 Share on other sites More sharing options...
allworknoplay Posted February 19, 2009 Share Posted February 19, 2009 I have session and also i got function for check if logged in. so now i need is the "if/else" to redirect. Would that change anything on the php code you inlcude in? if($user == admin) header('Location: http://www.whatever1.com/'); exit; if($user == client) header('Location: http://www.whatever2.com/'); exit; if($user == guest) header('Location: http://www.whatever3.com/'); exit; Quote Link to comment https://forums.phpfreaks.com/topic/145918-user-access-level/#findComment-766167 Share on other sites More sharing options...
premiso Posted February 19, 2009 Share Posted February 19, 2009 if($user == admin) header('Location: http://www.whatever1.com/'); exit; if($user == client) header('Location: http://www.whatever2.com/'); exit; if($user == guest) header('Location: http://www.whatever3.com/'); exit; That is very redundant. $levelRedirect = array("admin" => "http://yoursite.com/admin", "user" => "http://yoursite.com/user", "guest" => "http://yoursite.com/guest"); if (!in_array($userLevel, $levelRedirect)) $userLevel = "guest"; header ('Location: ' . $levelRedirect[$userLevel]); A bit cleaner with less code Quote Link to comment https://forums.phpfreaks.com/topic/145918-user-access-level/#findComment-766237 Share on other sites More sharing options...
allworknoplay Posted February 19, 2009 Share Posted February 19, 2009 if($user == admin) header('Location: http://www.whatever1.com/'); exit; if($user == client) header('Location: http://www.whatever2.com/'); exit; if($user == guest) header('Location: http://www.whatever3.com/'); exit; That is very redundant. $levelRedirect = array("admin" => "http://yoursite.com/admin", "user" => "http://yoursite.com/user", "guest" => "http://yoursite.com/guest"); if (!in_array($userLevel, $levelRedirect)) $userLevel = "guest"; header ('Location: ' . $levelRedirect[$userLevel]); A bit cleaner with less code Redundant? yes....cleaner? that's perspective.... For a newbie, it's easier for them to understand the code I wrote as it's function is in your face... Your code is good and I like it, but could confuse newbies..and requires knowledge of arrays... Quote Link to comment https://forums.phpfreaks.com/topic/145918-user-access-level/#findComment-766244 Share on other sites More sharing options...
premiso Posted February 19, 2009 Share Posted February 19, 2009 I am not trying to get into a pissing match at all. But in all honesty, the code you gave him is just poor code. Why do 3 ifs like that, when you can do if/elseif/else ? Even to try and "show", you still should show them the proper way, not some hodged podged way that only a newbie would use (sorry no offense intended). if ($user == 'admin') { header('Location: http://www.whatever1.com/'); elseif ($user == 'client') { header('Location: http://www.whatever2.com/'); }else { header('Location: http://www.whatever3.com/'); } exit; You also left off quotes around the variable checking, which does throw a NOTICE undefined constant error (not major but it is better practice to encapsulate the non-constant string in quotes). Like I said, my intentions were not to put you down. Another variation, just because I feel like it, is they could also use a switch: switch($user) { case 'admin': header('Location: http://www.whatever1.com/'); break; case 'client': header('Location: http://www.whatever2.com/'); break; case 'guest': default: header('Location: http://www.whatever3.com/'); break; } Now granted my initial way, I prefer to do cause it is easier for me, and I do understand that it can be confusing for a new user and yes I probably should of just written an else/if or a case/switch instead. It seems like you were more worried about being #1 to post than posting the proper method. I do that too. Just kinda making you aware of it. Showing them improper coding does not help them, just like me posting way advanced stuff that they might not understand. Quote Link to comment https://forums.phpfreaks.com/topic/145918-user-access-level/#findComment-766286 Share on other sites More sharing options...
allworknoplay Posted February 19, 2009 Share Posted February 19, 2009 I am not trying to get into a pissing match at all. But in all honesty, the code you gave him is just poor code. Why do 3 ifs like that, when you can do if/elseif/else ? Even to try and "show", you still should show them the proper way, not some hodged podged way that only a newbie would use (sorry no offense intended). if ($user == 'admin') { header('Location: http://www.whatever1.com/'); elseif ($user == 'client') { header('Location: http://www.whatever2.com/'); }else { header('Location: http://www.whatever3.com/'); } exit; You also left off quotes around the variable checking, which does throw a NOTICE undefined constant error (not major but it is better practice to encapsulate the non-constant string in quotes). Like I said, my intentions were not to put you down. Another variation, just because I feel like it, is they could also use a switch: switch($user) { case 'admin': header('Location: http://www.whatever1.com/'); break; case 'client': header('Location: http://www.whatever2.com/'); break; case 'guest': default: header('Location: http://www.whatever3.com/'); break; } Now granted my initial way, I prefer to do cause it is easier for me, and I do understand that it can be confusing for a new user and yes I probably should of just written an else/if or a case/switch instead. It seems like you were more worried about being #1 to post than posting the proper method. I do that too. Just kinda making you aware of it. Showing them improper coding does not help them, just like me posting way advanced stuff that they might not understand. Sure no problem. I am always for learning better techniques. Yes I wanted to get him the answer as soon as possible. The if/elseif/else would of course work which utilizes just 1 "exit" function...like I said, there's a lot of ways to do it... The way I wrote it is just one method. The extra "exit"'s that I put in of course was to make sure that none of the other header redirects would run.... Quote Link to comment https://forums.phpfreaks.com/topic/145918-user-access-level/#findComment-766308 Share on other sites More sharing options...
omfgthezerg Posted February 19, 2009 Share Posted February 19, 2009 if($user == admin) header('Location: http://www.whatever1.com/'); exit; if($user == client) header('Location: http://www.whatever2.com/'); exit; if($user == guest) header('Location: http://www.whatever3.com/'); exit; That is very redundant. $levelRedirect = array("admin" => "http://yoursite.com/admin", "user" => "http://yoursite.com/user", "guest" => "http://yoursite.com/guest"); if (!in_array($userLevel, $levelRedirect)) $userLevel = "guest"; header ('Location: ' . $levelRedirect[$userLevel]); A bit cleaner with less code $levelRedirect = array('admin' => 'http://yoursite.com/admin', 'user' => 'http://yoursite.com/user', 'guest' => 'http://yoursite.com/guest'); if (!in_array($userLevel, $levelRedirect)) $userLevel = 'guest'; header ('Location: '.$levelRedirect[$userLevel]); *cough* Single quotes are (yes marginally) faster than double quotes due to php not having to see if it needs to replace a variable (almost other things). Quote Link to comment https://forums.phpfreaks.com/topic/145918-user-access-level/#findComment-766331 Share on other sites More sharing options...
premiso Posted February 19, 2009 Share Posted February 19, 2009 *cough* Single quotes are (yes marginally) faster than double quotes due to php not having to see if it needs to replace a variable (almost other things). *ehrmmmemmmrmmm clears throat erhmhmmmm* Wow you must really be hurting to post something huh? lmao. That is just too funny. Yes, I know they are faster. In order to see a difference, however, you have to loop through about 50,000 rows to get a benchmark that you would even be able to notice a difference. But notice, I did not say "more efficient" only "A bit cleaner with less code". Quote Link to comment https://forums.phpfreaks.com/topic/145918-user-access-level/#findComment-766338 Share on other sites More sharing options...
omfgthezerg Posted February 19, 2009 Share Posted February 19, 2009 *cough* Single quotes are (yes marginally) faster than double quotes due to php not having to see if it needs to replace a variable (almost other things). *ehrmmmemmmrmmm clears throat erhmhmmmm* Wow you must really be hurting to post something huh? lmao. That is just too funny. Yes, I know they are faster. In order to see a difference, however, you have to loop through about 50,000 rows to get a benchmark that you would even be able to notice a difference. But notice, I did not say "more efficient" only "A bit cleaner with less code". Well I need some kind of distraction from work Quote Link to comment https://forums.phpfreaks.com/topic/145918-user-access-level/#findComment-766343 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.