最簡單的就是調用Wordpress函數wp_hash_password(password)。在這種情況下,您的查詢看起來像:
require_once '/path/to/wp-config.php'; //will load up wordpress, such as if you're doing this from a script
$SQL = "SELECT * FROM wp_users where user_login='".$username."' and user_pass='".wp_hash_password($password)."'";
的WordPress不使用標準MD5,而是phpass庫,哈希用戶密碼。所以,如果你想從Wordpress之外做到這一點,那麼你需要看看這個庫。下面是實現wp_hash_password()方法使用phpass代碼:
if (!function_exists('wp_hash_password')) :
/**
* Create a hash (encrypt) of a plain text password.
*
* For integration with other applications, this function can be overwritten to
* instead use the other package password checking algorithm.
*
* @since 2.5
* @global object $wp_hasher PHPass object
* @uses PasswordHash::HashPassword
*
* @param string $password Plain text user password to hash
* @return string The hash string of the password
*/
function wp_hash_password($password) {
global $wp_hasher;
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);
}
return $wp_hasher->HashPassword($password);
}
endif;
你可以按照相同的模式可能會得到相同的內容:
require_once '/path/to/wp-includes/class-phpass.php';
$my_hasher = new PasswordHash(8, TRUE);
$SQL = "SELECT * FROM wp_users where user_login='".$username."' and user_pass='". $my_hasher->HashPassword($password)."'";
當你說它返回false時,你如何測試這個?您是否嘗試將第二個查詢的輸出直接粘貼到php myadmin或mysql命令行中以查看它輸出的內容? –
查詢是正確的。如果返回0結果,則表示用戶名或密碼錯誤。只需'md5($ password)'並與db中的內容進行比較。 – sachleen
發佈數據庫散列密碼的示例。顯然,你需要爲此使用一些測試帳戶,而不是真正的用戶密碼。 –