PHP - 从另一个函数获取可变数据

时间:2018-01-21 14:16:03

标签: php mysql encryption pdo

我尝试使用AES-256加密密码,因为我拥有数据库中的密钥和方法。我的问题是我使用的是mysqli并且工作得非常好,但现在我正在用PDO制作其他mysqli并创建函数。

这些功能在没有加密功能的情况下运行良好。所以有我的代码。

<?php 

class DWLib {

public function Register($u_name, $u_email, $u_pass) {
        try {
            $db = DB();
            $query = $db->prepare("INSERT INTO users(u_fname, u_lname, u_name, u_email, u_pass, u_rank) VALUES ('','',:username,:email,:password,'1')");
            $query->bindParam("username", $u_name, PDO::PARAM_STR);
            $query->bindParam("email", $u_email, PDO::PARAM_STR);

            global $password;

            $password = substr(hash('sha256', $password, true), 0, 32);

            global $method;

            $iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);

            $encrypted = base64_encode(openssl_encrypt($u_pass, $method, $password, OPENSSL_RAW_DATA, $iv));

            $query->bindParam("password", $encrypted, PDO::PARAM_STR);
            $query->execute();
            return $db->lastInsertId();
        } catch (PDOException $e) {
            exit($e->getMessage());
        }
    }

  public function Encryption($password, $method) 
    {
        try {
            $db = DB();
            $query = $db->prepare("SELECT * FROM encryption");
            $query->execute();
            if ($query->rowCount() > 0) {
                while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
                    $password = $row["password"];
                    $method = $row["method"];
                }
            } else {
                return false;
            }
        } catch (PDOException $e) {
            exit($e->getMessage());
        }
    }
}

?>

我尝试使用return,但一直没有工作......

1 个答案:

答案 0 :(得分:0)

如果您想更新$password$method

您必须通过引用&传递它们:

public function Encryption(&$password, &$method) {

修改

如果必须从函数传递变量,则必须使用属性。添加private $password;private $method;,并将其与$this->password$this->method一起使用:

class DWLib {
    private $password;
    private $method; 

    public function function_1() {
       // ...
          $this->password = "something";
          $this->method = "something else";
       // ...
    }

    public function function_2() {
       // ...
          var_dump($this->password) ;
          var_dump($this->method) ; 
       // ...
    }

 }