Jump to content

using code to use database tables


avatar.alex

Recommended Posts

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?

Link to comment
Share on other sites

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 
}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)  {
?>

Link to comment
Share on other sites

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');
}
?>

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.