csteff24 Posted December 24, 2009 Share Posted December 24, 2009 In my members table, I have a field called "perm" and it's set to zero for all members. However, I have two administrators, and theirs are set to 1. I want my members page that shows up on login to redirect admins if their "perm" number is set to 1 Right now it's not having any errors, but it's not redirecting the admins. Thank you! Full code: <?php // Connects to your Database ... //checks cookies to make sure they are logged in if(isset($_COOKIE['ID_staples_clubs'])) { $username = $_COOKIE['ID_staples_clubs']; $pass = $_COOKIE['Key_staples_clubs']; $check = mysql_query("SELECT * FROM members WHERE username = '$username'")or die(mysql_error()); while($info = mysql_fetch_array( $check )) { //if the cookie has the wrong password, they are taken to the login page if ($pass != $info['password']) { header("Location: login.php"); } //otherwise they are shown the member area else { $query = "SELECT clubs.*, members.* FROM clubs LEFT JOIN members ON members.club = clubs.id WHERE members.username = '{$_COOKIE['ID_staples_clubs']}'"; $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { //admin if ($row['perm'] == 1) { header("Location: admin.php"); } //member else { echo "$row[name] <br />"; echo "Welcome, "; echo "$row[adv] <br />"; echo "<a href=edititem.php?id=$row[club]>Edit Club</a>"; echo "<br />"; echo "<a href=changepass.php>Change password</a><br />"; echo "<a href=logout.php>Logout</a>"; } } } } } else //if the cookie does not exist, they are taken to the login screen { header("Location: login.php"); } ?> Troublesome part: while ($row = mysql_fetch_array($result)) { //admin if ($row['perm'] == 1) { header("Location: admin.php"); } //member else { echo "$row[name] <br />"; echo "Welcome, "; echo "$row[adv] <br />"; echo "<a href=edititem.php?id=$row[club]>Edit Club</a>"; echo "<br />"; echo "<a href=changepass.php>Change password</a><br />"; echo "<a href=logout.php>Logout</a>"; } Quote Link to comment https://forums.phpfreaks.com/topic/186231-redirecting-admin/ Share on other sites More sharing options...
Buddski Posted December 24, 2009 Share Posted December 24, 2009 Do your admins have an associated club? Quote Link to comment https://forums.phpfreaks.com/topic/186231-redirecting-admin/#findComment-983529 Share on other sites More sharing options...
Psycho Posted December 24, 2009 Share Posted December 24, 2009 No need to do a second query to get the perm value, just grab it with the first query. Try this <?php // Connects to your Database ... //checks cookies to make sure they are logged in if(!isset($_COOKIE['ID_staples_clubs']) || !isset($_COOKIE['Key_staples_clubs'])) { //Cookie values not set, redirect to login header("Location: login.php"); } //Cookie values are set, run query $username = mysql_real_escape_string($_COOKIE['ID_staples_clubs']); $pass = mysql_real_escape_string($_COOKIE['Key_staples_clubs']); $query = "SELECT `perm` FROM `members` WHERE `username`='$username' AND `password`='{$pass}'"; $result = mysql_query($query)or die(mysql_error()); if (mysql_num_rows($result)!=1) { //No match for Username/password, redirect to login header("Location: login.php"); exit(); } //Check for admin $userInfo = mysql_fetch_assoc($result); if ($userInfo['perm']==1) { //Redirect to admin header("Location: admin.php"); exit(); } //Not an admin, show member page echo "{$userInfo['name']} <br />\n"; echo "Welcome, "; echo "{$userInfo['adv']} <br />\n"; echo "<a href=\"edititem.php?id={$userInfo['club']}\">Edit Club</a>"; echo "<br />\n"; echo "<a href=\"changepass.php\">Change password</a><br />\n"; echo "<a href=\"logout.php\">Logout</a>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/186231-redirecting-admin/#findComment-983535 Share on other sites More sharing options...
csteff24 Posted December 24, 2009 Author Share Posted December 24, 2009 Thank you! I tried that, but it's still not redirecting the admin for some reason... I set the perm field to be boolean, would that change anything? Quote Link to comment https://forums.phpfreaks.com/topic/186231-redirecting-admin/#findComment-983748 Share on other sites More sharing options...
csteff24 Posted December 24, 2009 Author Share Posted December 24, 2009 No, the admins don't have a club...it's just the username and password and perm value of 1 Quote Link to comment https://forums.phpfreaks.com/topic/186231-redirecting-admin/#findComment-983827 Share on other sites More sharing options...
Buddski Posted December 24, 2009 Share Posted December 24, 2009 Wll your query here $query = "SELECT clubs.*, members.* FROM clubs LEFT JOIN members ON members.club = clubs.id WHERE members.username = '{$_COOKIE['ID_staples_clubs']}'"; $result = mysql_query($query) or die(mysql_error()); Will only bring up members that have a club... try $query = "SELECT * FROM members LEFT JOIN clubs ON members.club = clubs.id WHERE members.username = '{$_COOKIE['ID_staples_clubs']}'"; $result = mysql_query($query) or die(mysql_error()); This query checks the members table first and joins to the clubs and will get a club if they have one or nothing if they dont.. Therefore the Admins would not be returned.. Your previous query got the clubs then looked at all the associated members for those clubs.. If you code is still as you had it originally it should work.. Quote Link to comment https://forums.phpfreaks.com/topic/186231-redirecting-admin/#findComment-983833 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.