Jump to content

Recommended Posts

At the minute I have a great login scrip that I use.

 

Here is the little snippet of code I use to check if they're logged in:

 

function loveLogged() // Function to determine if the sessioned variables are correct, and therefore logged in.
{
  $check = mysql_num_rows(mysql_query("SELECT * FROM ". MEMBER_TBL ." WHERE `username` = '".$_SESSION['username']."' AND `password` ='".$_SESSION['password']."'"));
  if ($check == 0) { return false; }
  else if ($check == 1) { return true; }
  else if ($check > 1) { return false; }
}

It's usablity in a page is:

loveLogged() { Logged in, do stuff }
else { Not logged in, display error }

It's all good. However, I'd like to add a little to this. For instance, I have different groups of members, the 2 highest groups have all privileges. I'd like to add to this function the ability to check to see if the user logged in is an admin or not and if s/he is then only show/parse the "logged in, do stuff" code  if they are actually an admin. Ex:

loveLogged(true) { Admin, do admin stuff }
else { Not logged in or not an admin, display error }

 

I'm not great with custom functions, but I'd like to be able to just add in "true" to the brackets for this to enable.

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/71271-solved-login-script-additions/
Share on other sites

You want something like this:

<?php
function adminCheck()
{
    $query = mysql_query("SELECT * FROM ". MEMBER_TBL ." WHERE `username` = '".$_SESSION['username']."' AND `password` ='".$_SESSION['password']."'"));
    if(mysql_num_rows($query > 0))
    {
        //User is logged in correctly
        $user = mysql_fetch_array($query);
        if($user['user_level'] = ADMIN) 
        {
            //User is an admin.
            return true;
        }
        else
        {
            //User isn't admin.
            return false;
        }
    }
    else
    {
        //User isn't logged in correctly.
        return false;
    }
}
?>

<?php
function loveLogged($admin)
{
$query = mysql_query("SELECT * FROM ". MEMBER_TBL ." WHERE `username` = '".$_SESSION['username']."' AND `password` ='".$_SESSION['password']."'"));
if(mysql_num_rows($query > 0)
{
	//User is logged in.
	if($admin == true)
	{
		//Test for admin
		$user = mysql_fetch_array($query);
		if($user['user_level'] = ADMIN)
		{
			//User is an admin
			return true;
		}
		else
		{
			//User isn't an admin.
			return false;
		}
	}
	else
	{
		//User doesn't need to be admin.
		return true;
	}
}
else
{
	//User isn't logged in.
	return false;
}
}

?>

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.