avatar.alex Posted January 4, 2010 Share Posted January 4, 2010 Ok I can't find it anywhere or know what its called. But I have my user management script coded for the most part. I have a table called groups and there are Standard users, mods, and Admin. If I wanted the page I choose to just be available for ADMIN how would I go about doing that? Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/ Share on other sites More sharing options...
mikesta707 Posted January 4, 2010 Share Posted January 4, 2010 what does your table setup look like? I would need that to give an exact answer, but you wanna do a query like $result = mysql_query("SELECT * FROM TABLE WHERE username = 'whatever variables holds username' AND rank='admin'"); if (mysql_num_rows($result) > 0){ //show page } Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/#findComment-988466 Share on other sites More sharing options...
avatar.alex Posted January 5, 2010 Author Share Posted January 5, 2010 OK I thought it would have worked but it didn't I have <?php $result = mysql_query("SELECT * FROM Users WHERE Group_ID = '2'"); if (mysql_num_rows($result) > 0){ ?> Admin page <?php } else { header("Location: index.php"); die; } ?> A standard user has a group id of "1" an administrator has a group id of "2". If the user doesn't have a group id of 2 they should be located to the index page. Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/#findComment-988494 Share on other sites More sharing options...
avatar.alex Posted January 5, 2010 Author Share Posted January 5, 2010 Ok I solved it. I set the $result > 1 not 0. Thank you so much Wait so I set my group id to 2 and its redirecting me to the index page anyways? Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/#findComment-988498 Share on other sites More sharing options...
avatar.alex Posted January 5, 2010 Author Share Posted January 5, 2010 could I use mysql_fetch_array in some way??? Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/#findComment-988517 Share on other sites More sharing options...
laffin Posted January 5, 2010 Share Posted January 5, 2010 if(!mysql_num_rows(mysql_query("SELECT * From Users WHERE id={$userid} and Group_id=2"))) { die('Restricted Page"); } ?> Admin Page Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/#findComment-988533 Share on other sites More sharing options...
avatar.alex Posted January 5, 2010 Author Share Posted January 5, 2010 if(!mysql_num_rows(mysql_query("SELECT * From Users WHERE id={$userid} and Group_id=2"))) { die('Restricted Page"); } ?> Admin Page im not using user id at all Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/#findComment-988536 Share on other sites More sharing options...
laffin Posted January 5, 2010 Share Posted January 5, 2010 exactly why it wont work otherwise. to get what group affilation a user belongs to, u need to pass both requirements. user_id and group_id SELECT * FROM Users WHERE Group_ID = '2' this just returns all users in group 2. so either you check each row to see if u find the users id or u put both in the query Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/#findComment-988540 Share on other sites More sharing options...
avatar.alex Posted January 5, 2010 Author Share Posted January 5, 2010 what if there are two admin? How would it work? Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/#findComment-988542 Share on other sites More sharing options...
RaythMistwalker Posted January 5, 2010 Share Posted January 5, 2010 atm the way that your checking if there is group_id in table will allow any user to access admin page due to the fact there will always be group_id 2. You need to set it so it checks the user has group id 2 Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/#findComment-988543 Share on other sites More sharing options...
avatar.alex Posted January 5, 2010 Author Share Posted January 5, 2010 ok. SO would I do $userid = mysql_query("SELECT * FROM Users WHERE User_ID"); Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/#findComment-988547 Share on other sites More sharing options...
RaythMistwalker Posted January 5, 2010 Share Posted January 5, 2010 when the user logs in you have to set somethin like a session variable $_SESSION['LOGIN'] = $login; then: $login=$_SESSION['LOGIN']; $usergroup = mysql_query("SELECT usergroup FROM Users WHERE login='$login'"); $result=mysql_query($usergroup) $group=mysql_result($result,0,"usergroup") if ($group == 2) { display page } else { redirect } Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/#findComment-988552 Share on other sites More sharing options...
avatar.alex Posted January 5, 2010 Author Share Posted January 5, 2010 My page is coming out blank. Im really sorry. I understand most of the code. It's just not working. Umm...What if a session is already in motion? Im sorry for being difficult. Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/#findComment-988557 Share on other sites More sharing options...
avatar.alex Posted January 5, 2010 Author Share Posted January 5, 2010 this is my session session_start(); //Global User Object Var //loggedInUser can be used globally if constructed if(isset($_SESSION["userCakeUser"]) && is_object($_SESSION["userCakeUser"])) $loggedInUser = $_SESSION["userCakeUser"]; else $loggedInUser = NULL; So I could take the $_SESSION["userCakeUser"]; and = it to $login? Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/#findComment-988559 Share on other sites More sharing options...
oni-kun Posted January 5, 2010 Share Posted January 5, 2010 this is my session session_start(); //Global User Object Var //loggedInUser can be used globally if constructed if(isset($_SESSION["userCakeUser"]) && is_object($_SESSION["userCakeUser"])) $loggedInUser = $_SESSION["userCakeUser"]; else $loggedInUser = NULL; So I could take the $_SESSION["userCakeUser"]; and = it to $login? Yes, if the 'userCakeUser' session variable is the same as what it'd be in the database. Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/#findComment-988563 Share on other sites More sharing options...
avatar.alex Posted January 5, 2010 Author Share Posted January 5, 2010 OK whats the usergroup for? would that be the Group_id or User_id or totally different? Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/#findComment-988566 Share on other sites More sharing options...
avatar.alex Posted January 5, 2010 Author Share Posted January 5, 2010 I have, but the whole page is blank? Not even a error? <?php $log = $_SESSION["userCakeUser"] $usergroup = mysql_query("SELECT Group_ID FROM userCake_Users WHERE login='$log'"); $result=mysql_query($usergroup) $group=mysql_result($result,0,"Group_ID") if ($group == 2) { ?> Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/#findComment-989247 Share on other sites More sharing options...
avatar.alex Posted January 6, 2010 Author Share Posted January 6, 2010 Ok this is what I have my page is just blank. It won't even show anything on view source. <?php $log = $_SESSION["userCakeUser"] $usergroup = mysql_query("SELECT Group_ID FROM userCake_Users WHERE login='$log'"); $result=mysql_query($usergroup) $group=mysql_result($result,0,"Group_ID") if ($group == 2) { ?> Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/#findComment-989262 Share on other sites More sharing options...
Tazerenix Posted January 6, 2010 Share Posted January 6, 2010 when you define your session on login add $_SESSION['groupID'] = mysql_result(mysql_query("SELECT Group_ID FROM userCake_users WHERE username = '$loggedinusername'"),0) Where $loggedinusername is the username the person used to log in. Then in the page you can use <?php if ($_SESSION['groupID'] >= 2) { ?>Admin Page <?php } else { header('Location: index.php'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/187186-using-code-to-use-database-tables/#findComment-989281 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.