Jump to content


Photo

Handling a Function's Return Value


  • Please log in to reply
4 replies to this topic

#1 tommyd

tommyd
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 24 May 2006 - 12:26 AM

I'm not that well versed in PHP, so please excuse my ignorance, but here's the problem I'm having.

I've got a simple member script, which consists of a login form, a members area, and an admin area. The script uses MySQL as a DB backend. Here's how it works:

1.) As a user attempts to go to the members area (by calling up members.php), they're redirected to the login (login.php) form if they've not yet logged in. Once they have a good session (i.e. - their user name exists, and their password matches with their username) they're redirected to the members area.

2.) members.php is loaded, and a simple check is performed against the database to see if the user is an administrator or not. If they are, a link to admin.php is printed at the top of the page. If they're not an admin, the page is loaded the same way, sans the link.

Now, I used to use the following block of code, and it worked just fine:

(NOTE: the "isadmin" column in the database is ALWAYS either "1" or "0", and there is a connection to the database already open at the beginning of the script.)


// Make things easier...

$loginname = "".$_SESSION["login"]."";

// Now, we need to check if the user is an administrator or not...
// Let's pull that info from the DB, shall we?

$admsql = "SELECT isadmin FROM users WHERE login = '$loginname'";
$admresult = mysql_query($admsql) or die(mysql_error());
if (mysql_num_rows($admresult) == 1) {
$isadmin = mysql_result($admresult, 0, 'isadmin');
} else {
echo "(There was an error fetching this, please contact an admin ASAP.)";
}

// Ok, now that we have the value, let's print a link to admin.php
// in the links on top of the page is the user is an admin.
// If they're not, we'll print out the header sans that link...

if ($isadmin == 0) {
print("<center><h2><b>".$_SESSION["login"]."'s Member Area</b></h2>\n");
print("<a href=\"logout.php?".session_name()."=".session_id()."\">Logout</a></center>");
print("Welcome to your member area, <b>".$_SESSION["login"]."</b><br><br>\n");
}
else {
print("<center><h2><b>".$_SESSION["login"]."'s Member Area</b></h2>\n");
print("<a href=\"admin.php\">Go to the Admin Area</a> | <a href=\"logout.php?".session_name()."=".session_id()."\">Logout</a><br><br></center>");
print("Welcome to your member area, <b>".$_SESSION["login"]."</b><br><br>\n");
}


That's all well and good, but I realized that I could use the same code throughout the site for different features, so I decided to try and make a function, which kind of blew up in my face. Here's the aborted function code:

function isAdmin($loginname) {
    global $link;

    $admquery = "SELECT isadmin FROM users WHERE login = '$loginname'";
    $admresult = mysql_query($admquery) or die(mysql_error());
    if ($admresult == 1) {
    return true;
    } 
    else { return false; }
} // end func isAdmin($loginname)


Right now, I'm trying to handle things in the areas I call the function like this:

if (isAdmin($loginname) == TRUE) {
print out the header with the link
}
else {
don't print the header with the link
}

I call the function using isadmin($loginname); in the script(s) as shown above, but now all members get the link on the top of their members area (the link to admin.php), and all users can access the admin script as well.

So, I guess my question is, how do I fix my function to make it do the same thing as my original code block? I'm kind of lost here.

Thanks in advance,

--Tom

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 24 May 2006 - 12:37 AM

try this:

if (($blah = isAdmin($loginname)) == TRUE) {
print out the header with the link
}
else {
don't print the header with the link
}

or

$blah = isAdmin($loginname);
if ($blah == TRUE) {
print out the header with the link
}
else {
don't print the header with the link
}
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 tommyd

tommyd
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 24 May 2006 - 02:58 AM

[!--quoteo(post=376519:date=May 23 2006, 08:37 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 23 2006, 08:37 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
try this:

if (($blah = isAdmin($loginname)) == TRUE) {
print out the header with the link
}
else {
don't print the header with the link
}

or

$blah = isAdmin($loginname);
if ($blah == TRUE) {
print out the header with the link
}
else {
don't print the header with the link
}
[/quote]

Unfortunately, I get the same problem...everyone has access to the link and the admin.php script using that... [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /]


#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 24 May 2006 - 03:12 AM

not entirely sure about this, but in your function you use lowercase true/false but in your call to it you check to see if it is TRUE/FALSE (caps). maybe they need to be the same?

also, hows about trying to return an actual variable as true or false in your function?

like

if ($admresult == 1) {
$valid = true;
return $valid;
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 tommyd

tommyd
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 24 May 2006 - 06:01 AM

[!--quoteo(post=376545:date=May 23 2006, 11:12 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 23 2006, 11:12 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
not entirely sure about this, but in your function you use lowercase true/false but in your call to it you check to see if it is TRUE/FALSE (caps). maybe they need to be the same?

also, hows about trying to return an actual variable as true or false in your function?

like

if ($admresult == 1) {
$valid = true;
return $valid;
[/quote]

~AHA!~ That first bit doesn't really matter (or it didn't), but that second part did...here's what eventually worked for me, for reference:

The isAdmin(); Function:
function isAdmin($loginname) {
    global $link;


    $admsql = "SELECT isadmin FROM users WHERE login = '$loginname'";
    $admresult = mysql_query($admsql) or die(mysql_error());
    if (mysql_num_rows($admresult) == 1) {
        $isadmin = mysql_result($admresult, 0, 'isadmin');
        return $isadmin;
    } else {
        return FALSE;
    }
} // end func isAdmin($loginname)

How I utilized it in the members area:
isadmin = isAdmin($loginname);
if ($isadmin == 0) {
print("<h2><b>".$_SESSION["login"]."'s Member Area</b></h2>\n");
print("<a href=\"logout.php?".session_name()."=".session_id()."\">Logout</a></center>");
print("Welcome to your member area, <b>".$_SESSION["login"]."</b><br><br>\n");
}
else {
print("<h2><b>".$_SESSION["login"]."'s Point Summary</b></h2>\n");
print("<a href=\"admin.php\">Go to the Admin Area</a> | <a href=\"logout.php?".session_name()."=".session_id()."\">Logout</a><br><br></center>");
print("Welcome to your member area, <b>".$_SESSION["login"]."</b><br><br>\n");
}


Thanks alot, crayon! :-)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users