Jump to content

return false and error?


ohdang888

Recommended Posts

<?php

function log_in($email, $password){

if(empty($email) and empty($password)){
$error = "Empty username or password";
}

$email = clean($email);
$password = md5(md5(clean($password)));
db("discuss");
$res = sql("SELECT * FROM `users` WHERE upper(email)=upper('$email') and `password`='$password' ");
if(mysql_num_rows($res) == 1){
$row = mysql_fetch_array($res);
$_SESSION['logged_in'] = true;
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['username'] = $row['username'];
$_SESSION['key'] = $row['key'];

//grab friends and put it in session
if($row['user_id'] == 1){//if user id is 1
$_SESSION['admin'] = true;
}

$time =  date("Y-m-d H:i:s");
$id = $row['user_id'];
sql("DELETE FROM `online_now` WHERE `user_id`='$id' LIMIT 1");
sql("INSERT INTO `online_now` (`user_id`, `time`)VALUES('$id', '$time')");


}else{//end if one row is found
$res = sql("SELECT `user_id` FROM `users` WHERE `email`='$email' ");
if(mysql_num_rows($res) == 0){
$error = "Email does not exist";
}else{
$error = "Wrong Password";
}
}//end else
if(isset($error)){
return false;
return $error;
}else{
return true;
}
}//end log in
?>

Link to comment
Share on other sites

If I where you I would take a look at exceptions. I would also consider breaking that function down into smaller functions, it does far more than simply log in a user, hence your need for more detailed error messages. Functions should be short, concise and to the point.

Link to comment
Share on other sites

<?php

function log_in($email, $password){

if(empty($email) and empty($password)){
$error = "Empty username or password";
}

$email = clean($email);
$password = md5(md5(clean($password)));
db("discuss");
$res = sql("SELECT * FROM `users` WHERE upper(email)=upper('$email') and `password`='$password' ");
if(mysql_num_rows($res) == 1){
$row = mysql_fetch_array($res);
$_SESSION['logged_in'] = true;
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['username'] = $row['username'];
$_SESSION['key'] = $row['key'];

//grab friends and put it in session
if($row['user_id'] == 1){//if user id is 1
$_SESSION['admin'] = true;
}

$time =  date("Y-m-d H:i:s");
$id = $row['user_id'];
sql("DELETE FROM `online_now` WHERE `user_id`='$id' LIMIT 1");
sql("INSERT INTO `online_now` (`user_id`, `time`)VALUES('$id', '$time')");


}else{//end if one row is found
$res = sql("SELECT `user_id` FROM `users` WHERE `email`='$email' ");
if(mysql_num_rows($res) == 0){
$error = "Email does not exist";
}else{
$error = "Wrong Password";
}
}//end else
if(isset($error)){
return false;
return $error;
}else{
return true;
}
}//end log in
?>

 

Ok do this

 

<?php

function log_in($email, $password){
    if(empty($email) and empty($password))
    {
        $error = "Empty username or password";
    }

    $email = clean($email);
    $password = md5(md5(clean($password)));
    db("discuss");
    $res = sql("SELECT * FROM `users` WHERE upper(email)=upper('$email') and `password`='$password' ");
    if(mysql_num_rows($res) == 1)
    {
        $row = mysql_fetch_array($res);
        $_SESSION['logged_in'] = true;
        $_SESSION['user_id'] = $row['user_id'];
        $_SESSION['username'] = $row['username'];
        $_SESSION['key'] = $row['key'];

        //grab friends and put it in session
        if($row['user_id'] == 1)
        {//if user id is 1
            $_SESSION['admin'] = true;
        }

        $time =  date("Y-m-d H:i:s");
        $id = $row['user_id'];
        sql("DELETE FROM `online_now` WHERE `user_id`='$id' LIMIT 1");
        sql("INSERT INTO `online_now` (`user_id`, `time`)VALUES('$id', '$time')");
    }
    else
    {//end if one row is found
        $res = sql("SELECT `user_id` FROM `users` WHERE `email`='$email' ");
        if(mysql_num_rows($res) == 0){
            $error = "Email does not exist";
        }
        else
        {
            $error = "Wrong Password";
        }
    }//end else
    
    if(isset($error)){
        return $error;
    }
    else
    {
        return true;
    }
}//end log in
?>

 

Then check your function

 

<?php
if(log_in(blah, blah) !== true)
{
    /** Function is not true, so it contains our error message */
    echo log_in(blah, blah);
}
else
{
    /** Function returned true */
    echo "Success!";
}
?>

 

if it's

Link to comment
Share on other sites

that would work

 

Although you can only use it once and for only one function per script.

 

If you die...so to speak.  The script terminates

 

on the contrary though you can do this

 

function someFunction() {
   return false;
}
someFunction() or print("This function has failed by god");

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.