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

时间:2015-04-16 09:55:31

标签: php wordpress

我在Wordpress中创建了一个php文件,使用Hash& amp;盐加密方法,但我的加密密码与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;
    }

}

1 个答案:

答案 0 :(得分:1)

是的,不要开始为此编写自己的代码。 Wordpress要复杂得多。在你的php文件中包含wordpress并尝试:<?php wp_create_user( $username, $password, $email ); ?>

此处有更多详情:https://codex.wordpress.org/Function_Reference/wp_create_user