LemonInflux Posted October 26, 2007 Share Posted October 26, 2007 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; } ?> Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted October 26, 2007 Share Posted October 26, 2007 wrong syntax if(auth($user, $pass) = 1){ do this } should be if(auth($user, $pass) == 1){ do this } Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted October 26, 2007 Author Share Posted October 26, 2007 Woah, that did it. I feel kinda stupid now =X. Topic solved. Quote Link to comment Share on other sites More sharing options...
premiso Posted October 26, 2007 Share Posted October 26, 2007 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. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted October 26, 2007 Share Posted October 26, 2007 Well its a user function he might wanna perform other stuff with 2,3 etc.... so being specific always helped Quote Link to comment Share on other sites More sharing options...
premiso Posted October 26, 2007 Share Posted October 26, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.