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
https://forums.phpfreaks.com/topic/74885-solved-auth-problem/
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
https://forums.phpfreaks.com/topic/74885-solved-auth-problem/#findComment-378622
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
https://forums.phpfreaks.com/topic/74885-solved-auth-problem/#findComment-378692
Share on other sites

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.