• Welcome to ForumKorner!
    Join today and become a part of the community.

[Tutorial] Logging in with email address

Simple

Active Member
Reputation
0
Please make sure you read the entire post before asking for help. I've posted the original function so that you can see the differences in the event these functions change you can manually apply the differences.

If you wish to allow logging in with email address but not username

In functions_user.php:

PHP:
/**
 * Checks a password with a supplied username.
 *
 * @param string The username of the user.
 * @param string The plain-text password.
 * @return boolean|array False when no match, array with user info when match.
 */
function validate_password_from_username($username, $password)
{
    global $db;

    $query = $db->simple_select("users", "uid,username,password,salt,loginkey,coppauser,usergroup", "LOWER(username)='".$db->escape_string(my_strtolower($username))."'", array('limit' => 1));

    $user = $db->fetch_array($query);
    if(!$user['uid'])
    {
        return false;
    }
    else
    {
        return validate_password_from_uid($user['uid'], $password, $user);
    }
}

Becomes:

PHP:
/**
 * Checks a password with a supplied username.
 *
 * @param string The username of the user.
 * @param string The plain-text password.
 * @return boolean|array False when no match, array with user info when match.
 */
function validate_password_from_username($username, $password)
{
    global $db;

    $query = $db->simple_select("users", "uid,username,email,password,salt,loginkey,coppauser,usergroup", "LOWER(email)='".$db->escape_string(my_strtolower($username))."'", array('limit' => 1));

    $user = $db->fetch_array($query);
    if(!$user['uid'])
    {
        return false;
    }
    else
    {
        return validate_password_from_uid($user['uid'], $password, $user);
    }
}

If you wish to allow logging in with email address or username

In functions_user.php:

PHP:
/**
 * Checks a password with a supplied username.
 *
 * @param string The username of the user.
 * @param string The plain-text password.
 * @return boolean|array False when no match, array with user info when match.
 */
function validate_password_from_username($username, $password)
{
    global $db;

    $query = $db->simple_select("users", "uid,username,password,salt,loginkey,coppauser,usergroup", "LOWER(username)='".$db->escape_string(my_strtolower($username))."'", array('limit' => 1));

    $user = $db->fetch_array($query);
    if(!$user['uid'])
    {
        return false;
    }
    else
    {
        return validate_password_from_uid($user['uid'], $password, $user);
    }
}

Becomes:

PHP:
* Checks a password with a supplied username.
 *
 * @param string The username of the user.
 * @param string The plain-text password.
 * @return boolean|array False when no match, array with user info when match.
 */
function validate_password_from_username($username, $password)
{
    global $db;

    $query = $db->simple_select("users", "uid,username,email,password,salt,loginkey,coppauser,usergroup", "LOWER(username)='".$db->escape_string(my_strtolower($username))."' OR LOWER(email)='".$db->escape_string(my_strtolower($username))."'", array('limit' => 1));

    $user = $db->fetch_array($query);
    if(!$user['uid'])
    {
        return false;
    }
    else
    {
        return validate_password_from_uid($user['uid'], $password, $user);
    }
}

For both

In functions_user.php:

PHP:
/**
 * Checks if $username already exists in the database.
 *
 * @param string The username for check for.
 * @return boolean True when exists, false when not.
 */
function username_exists($username)
{
        global $db;

        $query = $db->simple_select("users", "COUNT(*) as user", "LOWER(username)='".$db->escape_string(my_strtolower($username))."'", array('limit' => 1));

        if($db->fetch_field($query, 'user') == 1)
        {
                return true;
        }
        else
        {   
                return false;
        }
}

Becomes:

PHP:
/**
 * Checks if $username already exists in the database.
 *
 * @param string The username for check for.
 * @return boolean True when exists, false when not.
 */
function username_exists($username)
{
    global $db;

    $query = $db->simple_select("users", "COUNT(*) as user,email,username", "LOWER(username)='".$db->escape_string(my_strtolower($username))."' OR LOWER(email)='".$db->escape_string(my_strtolower($username))."'", array('limit' => 1));

    if($db->fetch_field($query, 'user') == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
Top