简体   繁体   English

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

[英]Register user with wordpress encryption password in PHP

I had create a php file in Wordpress which register using the Hash & salt encryption method but my encryption password is different to wordpress which cause me unable to login due to different type of encryption password. 我已经在Wordpress中创建了一个php文件,该文件使用哈希和盐加密方法进行注册,但是我的加密密码与wordpress不同,由于不同类型的加密密码,导致我无法登录。 May i know how to encrypt and retrieve the password same like Wordpress does ? 我可以像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;
    }

}

Yeah dont start writing your own code for this. 是的,不要开始为此编写自己的代码。 Wordpress is much more sophisticated. WordPress更加复杂。

Include wordpress in your php file and try: <?php wp_create_user( $username, $password, $email ); ?> 在您的php文件中包括wordpress并尝试: <?php wp_create_user( $username, $password, $email ); ?> <?php wp_create_user( $username, $password, $email ); ?>

More details here: https://codex.wordpress.org/Function_Reference/wp_create_user 此处有更多详细信息: https : //codex.wordpress.org/Function_Reference/wp_create_user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM