Jump to content

Redirecting Admin


csteff24

Recommended Posts

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>";
}

Link to comment
Share on other sites

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";

?> 

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.