Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am working with site that is done with WordPress, and I need to add some parts that are outside WP, and to check user login, Logging users outside WP.

I tried with md5 of password but it's not...

I tried this code:

require_once( 'wp-includes/class-phpass.php' );
$wp_hasher = new PasswordHash( 8, TRUE );
$password = "passwordhere";
$hashed_password = $wp_hasher->HashPassword( $password );
$encryptedpass = md5($hashed_password);

But this is only for first time creating password, and it's always different.

I need code that can be used in this:

SELECT * FROM wp_customers WHERE email = "ccc@aaa.com" AND password = "<what goes here?>"

Is this possible anyhow?

Thanks.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Based on your other question ... it sounds like you're trying to validate a given plaintext password against what's stored in the database. Here's the function WordPress uses to do just that:

function wp_check_password($password, $hash, $user_id = '') {
    global $wp_hasher;

    // If the hash is still md5...
    if ( strlen($hash) <= 32 ) {
        $check = ( $hash == md5($password) );
        if ( $check && $user_id ) {
            // Rehash using new hash.
            wp_set_password($password, $user_id);
            $hash = wp_hash_password($password);
        }

        return apply_filters('check_password', $check, $password, $hash, $user_id);
    }

    // If the stored hash is longer than an MD5, presume the
    // new style phpass portable hash.
    if ( empty($wp_hasher) ) {
        require_once ( ABSPATH . 'wp-includes/class-phpass.php');
        // By default, use the portable hash from phpass
        $wp_hasher = new PasswordHash(8, TRUE);
    }

    $check = $wp_hasher->CheckPassword($password, $hash);

    return apply_filters('check_password', $check, $password, $hash, $user_id);
}

First, this plugin checks to see if the MD5 hash of the given password is the same as the stored (hashed) password for a user. It also checks to see if the PHPass hash of the given password is the same as the stored password for a user.

You can follow a similar pattern.

So let's say you're given a username and a password from the user and you want to validate them (my_password_validation( $username, $password )). You'll use the given username to pull a hashed password from the database. Then you compare the hash of the given password to the stored value to see if it's valid.

Here's some untested psuedocode:

function my_password_validation( $username, $password ) {
    // Select the users's password hash from the database
    $stored = query( 'SELECT * FROM wp_customers WHERE email = ' . $username );

    require_one( 'class-phpass.php' );
    $hasher = new PasswordHash(8, TRUE);

    return $hasher->CheckPassword( $password, $stored );
}

If the password you pass in to the function hashes to the same as the stored value, the function will return true. Otherwise it will return false.

Looking at the comments you left on the other question, it seems like you have some other issues, though. To quote:

So I get:

$P$BqVYujC/jqNY4aylZpHi475jwcaSUs1 But how can I compare that with one in DB?

One in DB is:

fa063a4ed35e092a2d4e15c1b6a61871 How to compare those two with MySQL?

I can tell you right now that the password you're getting from the database was not hashed using the PHPass utility. Those hashes will always resemble the $P$B starting because that's what tells the system how it was hashed. PHPass is based on Blowfish which uses that kind of a prefix on encrypted strings.

Your fa063... hash looks more like a standard MD5 hash ... so if your MD5 hash of the plaintext doesn't match, then I think you might have the wrong password.

To answer your "how to I compare those two with MySQL" question ... you don't. MySQL is the data store ... don't do any business logic or comparison in the data store. Read data out, then use a PHP script to perform your comparison.

share|improve this answer

It's very easy..

<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/wp-includes/class-phpass.php' );

// prepare database connection
$ip_address="localhost";
$user_db="userdb";
$pass_db="passdb";

$conn= mysql_connect($ip_address,$user_db,$pass_db);
mysql_select_db("dbname",$conn);
if (!$conn){
            echo "Could not connect: " . mysql_error();
            exit();
}

// wordpress' username that his password going to compare
$user = 'test';
$user_name = htmlspecialchars($user,ENT_QUOTES);

// plain password to compare
$password = 'tespass';

$hasher = new PasswordHash(8, TRUE);

// get user_name's hashed password from wordpress database
$queryx = "select * from wa1gty5f_users where user_login='$user_name'";
$Resultx = mysql_query($queryx,$conn);

while($row = mysql_fetch_array($Resultx)){
     $passnya = $row[user_pass];
}

// compare plain password with hashed password
if ($hasher->CheckPassword( $password, $passnya )){
    echo "MATCHED";
} else {
    echo "NO MATCHED";
}
?>
share|improve this answer
    
PasswordHash is not a core php class, so if you don't include it, you get an error. Remember that user asked to validate password outside WordPress, so WP classes and functions are not available. –  G. M. Sep 25 '13 at 4:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.