Jump to content

Redirect after login based on given permission


Evolver

Recommended Posts

Hello,

 

MySQL 4.1 / PHP 5.2

 

I'm a newbie to MySQL and novice to PHP.

 

I've managed to setup my site to authenticate users:

 

function login($username, $password)
// check username and password with db
// if yes, return true
// else throw exception
{
  // connect to db
  $conn = db_connect();

  // check if username is unique
  $result = $conn->query("select * from user 
                         where username='$username'
                         and password = sha1('$password')");
  
  if (!$result)
     throw new Exception('Could not log you in.');
  
  if ($result->num_rows>0)
     return true;
  else 
     throw new Exception('Could not log you in.');
}

 

I've added a column to the database in user called permission. I have 2 settings for permission, 1 for admin and 2 for all other users.

 

I simply want to be able to redirect the user after login to either the admin section or the member section.

 

Does anyone have any suggestions on how i can do this?

 

I attempted to create a php variable called $permission based on the user's permission setting in MySQL but alas this is where my newbiness is shining.

 

Here is what i came up with:

 

function findPerm($username, $password)
{
  //extract from the database the permission this user has stored
  $conn = db_connect();
  $result = $conn->query( "select permission
                          from user
                          where username = '$username'
					  and password = sha1('$password')");
  if (!$result)
    return false; 

  $row = $result->fetch_row();  
  return $row['permission'];
}

 

This didn't work when i tried to use the following code to display the permission setting after login:

findPerm();
echo $permission;

 

(In retrospect perhaps i can just assign a permission to admin and NULL to members so i won't have to worry about inserting anything for them later. This is just what i have come up with so far, i'm open to suggestions!)

 

Any help would be appreciated!

 

Thanks

Archived

This topic is now archived and is closed to further replies.

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