在PHP中使用wordpress加密密码注册用户



我在Wordpress中创建了一个php文件,该文件使用Hash和salt加密方法注册,但我的加密密码与wordpress不同,由于不同类型的加密密码,导致我无法登录。 我可以知道如何像Wordpress一样加密和检索密码吗?

public function storeUser($name, $email, $password) {
        $uuid = uniqid('', true);
        $hash = $this->hashSSHA($password);
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt = $hash["salt"]; // salt
        $result = mysql_query("INSERT INTO wp_users(unique_id, user_login, user_email, user_pass, salt, user_registered) VALUES('$uuid', '$name', '$name', '$encrypted_password', '$salt', NOW())");
        // check for successful store
        if ($result) {
            // get user details 
            $uid = mysql_insert_id(); // last inserted id
            $result = mysql_query("SELECT * FROM wp_users WHERE ID = $uid");
            // return user details
            return mysql_fetch_array($result);
        } else {
            return false;
        }
    }
    /**
     * Get user by email and password
     */
    public function getUserByEmailAndPassword($name, $password) {
        $result = mysql_query("SELECT * FROM wp_users WHERE user_login = '$name'") or die(mysql_error());
        // check for result $email
        $no_of_rows = mysql_num_rows($result);
        if ($no_of_rows > 0) {
            $result = mysql_fetch_array($result);
            $salt = $result['salt'];
            $encrypted_password = $result['user_pass'];
            $hash = $this->checkhashSSHA($salt, $password);
            // check for password equality
            if ($encrypted_password == $hash) {
                // user authentication details are correct
                return $result;
            }
        } else {
            // user not found
            return false;
        }
    }
    /**
     * Check user is existed or not
     */
    public function isUserExisted($name) {
        $result = mysql_query("SELECT user_login from wp_users WHERE user_login = '$name'");
        $no_of_rows = mysql_num_rows($result);
        if ($no_of_rows > 0) {
            // user existed 
            return true;
        } else {
            // user not existed
            return false;
        }
    }
    /**
     * Encrypting password
     * @param password
     * returns salt and encrypted password
     */
    public function hashSSHA($password) {
        $salt = sha1(rand());
        $salt = substr($salt, 0, 10);
        $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
        $hash = array("salt" => $salt, "encrypted" => $encrypted);
        return $hash;
    }
    /**
     * Decrypting password
     * @param salt, password
     * returns hash string
     */
    public function checkhashSSHA($salt, $password) {
        $hash = base64_encode(sha1($password . $salt, true) . $salt);
        return $hash;
    }
}

是的,不要为此开始编写自己的代码。WordPress要复杂得多。

在你的php文件中包括wordpress,然后尝试:<?php wp_create_user( $username, $password, $email ); ?>

更多详情请见:https://codex.wordpress.org/Function_Reference/wp_create_user

最新更新