conker87 Posted September 30, 2007 Share Posted September 30, 2007 At the minute I have a great login scrip that I use. Here is the little snippet of code I use to check if they're logged in: function loveLogged() // Function to determine if the sessioned variables are correct, and therefore logged in. { $check = mysql_num_rows(mysql_query("SELECT * FROM ". MEMBER_TBL ." WHERE `username` = '".$_SESSION['username']."' AND `password` ='".$_SESSION['password']."'")); if ($check == 0) { return false; } else if ($check == 1) { return true; } else if ($check > 1) { return false; } } It's usablity in a page is: loveLogged() { Logged in, do stuff } else { Not logged in, display error } It's all good. However, I'd like to add a little to this. For instance, I have different groups of members, the 2 highest groups have all privileges. I'd like to add to this function the ability to check to see if the user logged in is an admin or not and if s/he is then only show/parse the "logged in, do stuff" code if they are actually an admin. Ex: loveLogged(true) { Admin, do admin stuff } else { Not logged in or not an admin, display error } I'm not great with custom functions, but I'd like to be able to just add in "true" to the brackets for this to enable. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/71271-solved-login-script-additions/ Share on other sites More sharing options...
Cagecrawler Posted September 30, 2007 Share Posted September 30, 2007 You want something like this: <?php function adminCheck() { $query = mysql_query("SELECT * FROM ". MEMBER_TBL ." WHERE `username` = '".$_SESSION['username']."' AND `password` ='".$_SESSION['password']."'")); if(mysql_num_rows($query > 0)) { //User is logged in correctly $user = mysql_fetch_array($query); if($user['user_level'] = ADMIN) { //User is an admin. return true; } else { //User isn't admin. return false; } } else { //User isn't logged in correctly. return false; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71271-solved-login-script-additions/#findComment-358526 Share on other sites More sharing options...
conker87 Posted September 30, 2007 Author Share Posted September 30, 2007 That looks great. But is there anyway of incorporating this into the original function with that can be enabled with a 'true' in the brackets? Quote Link to comment https://forums.phpfreaks.com/topic/71271-solved-login-script-additions/#findComment-358537 Share on other sites More sharing options...
Cagecrawler Posted October 1, 2007 Share Posted October 1, 2007 <?php function loveLogged($admin) { $query = mysql_query("SELECT * FROM ". MEMBER_TBL ." WHERE `username` = '".$_SESSION['username']."' AND `password` ='".$_SESSION['password']."'")); if(mysql_num_rows($query > 0) { //User is logged in. if($admin == true) { //Test for admin $user = mysql_fetch_array($query); if($user['user_level'] = ADMIN) { //User is an admin return true; } else { //User isn't an admin. return false; } } else { //User doesn't need to be admin. return true; } } else { //User isn't logged in. return false; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71271-solved-login-script-additions/#findComment-358771 Share on other sites More sharing options...
cooldude832 Posted October 1, 2007 Share Posted October 1, 2007 select * is never a good idea, when in doubt run more q ueries than run 1 big one. Quote Link to comment https://forums.phpfreaks.com/topic/71271-solved-login-script-additions/#findComment-358777 Share on other sites More sharing options...
conker87 Posted October 1, 2007 Author Share Posted October 1, 2007 ... That's looks pretty sexy, thanks. @cooldude: I guess I should just select the username, password and group fields from it then? Quote Link to comment https://forums.phpfreaks.com/topic/71271-solved-login-script-additions/#findComment-358999 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.