您好我正在尝试制作一个检索密码脚本,我在数据库中的密码是编码的,这里是检索它的脚本:
<?php
$salt = "Zo4rU5Z1YyKJAASY0PT6EUg7BBYdlEhPaNLuxAwU8lqu1ElzHv0Ri7EM6irpx5w";
include_once("config.php"); //include the settings/configuration
$password = null;
/* function svarzcane kam bazata */
$email = $_POST['email'];
try {
$con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); //our new PDO Object
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch (PDOException $e) {
echo $e->getMessage(); //catch and show the error
}
$stmt = $con->prepare("SELECT password FROM users WHERE email = :getmail LIMIT 1");
$stmt->bindParam(":getmail", $_POST['email']);
$stmt->bindValue( "password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR );
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
while( $row = $stmt->fetch()) {
$pass = $row['password'];
}
这里的问题是,当我尝试解码密码时,它给了我一个错误:Fatal error: Using $this when not in object context in /home/cedecapr/public_html/retrive.php on line 18
我该如何解决?任何帮助将不胜感激。谢谢。
解码变量在类文件中定义:public $salt = "decoding code";
正如 Barmer 所建议的,我不能在课程代码之外使用它。而不是如何使用哈希算法绑定值。
如何更改此行
$stmt->bindValue( "password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR );
从数据库中解码结果。哈希算法也在代码顶部更新。