Jump to content

[SOLVED] Auth Problem


LemonInflux

Recommended Posts

Quick, simple script I wrote, but there's a problem with it:

 

Fatal error: Can't use function return value in write context in C:\xampp\htdocs\forumbuild\fns\page_fns.php on line 12

 

 

Here's the code. Basically, it's meant to return $result so you can say,

if(auth($user, $pass) = 1){ do this }

 

<?php
function auth($user, $pass){
$sql = mysql_query("SELECT * FROM `". MEMBERS ."` WHERE `username` = '". $user ."' AND `password` = '". md5($pass) ."'");
if(mysql_num_rows($sql) == 1){ // <-- Line 12
$result = 1;
}else{
$result = 0;
}
return $result;
}
?>

Link to comment
Share on other sites

Why even say == 1?

 

<?php
function auth($user, $pass){
     $sql = mysql_query("SELECT * FROM `". MEMBERS ."` WHERE `username` = '". $user ."' AND `password` = '". md5($pass) ."'");
     return (mysql_num_rows($sql) == 1)?true:false;
}

if (auth($user, $pass)) {
   echo "Welcome $user";
}else {
   echo "Not logged in.";
}
?>

 

A bit less coding meaning less chance for messing up. The return part is called the ternary operator which acts like an if/else in a short form.

Link to comment
Share on other sites

Well its a user function he might wanna perform other stuff with 2,3 etc.... so being specific always helped

 

If he wants to add an access level that would work, but it seemed to me he just wanted to say, "Is this user and password correct?".

 

Checking if a user has proper access level, well he would probably want to return the access instead if that is true this would probably be a better way:

 

<?php
function auth($user, $pass){
     $sql = mysql_fetch_assoc(mysql_query("SELECT username, access FROM `". MEMBERS ."` WHERE `username` = '". $user ."' AND `password` = '". md5($pass) ."'"));
     return ($sql['username'] == $user)?$sql['access']:false;
}

if (auth($user, $pass)== 3) {
   echo "Welcome Admin $user";
}elseif (auth($user, $pass) ==2) {
   echo "Welcome $user";
}else{
   echo "Not logged in.";
}
?>

 

Either way, it is always nice to have more than one solution to choose from for what might suit their needs, and make their life easier.

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.