尝试解码 PDO 对象时出错


Error when trying to decore PDO Object

您好,我正在尝试制作一个检索密码脚本,我在数据库中的密码已编码,这是用于检索它的脚本:

<?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";

正如巴默所建议的那样,我不能在类代码旁边使用它。 而不是如何将值与哈希算法绑定。

如何更改此行

$stmt->bindValue( "password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR );

以解码数据库中的结果。哈希算法在代码顶部更新为wel。

您可以完全删除此行:

$stmt->bindValue( "password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR );

您的 SQL 语句不包含名为 :password 的参数,因此无需向其绑定任何值。我猜你确实从某个类方法复制和粘贴了这行代码,其中$this有意义。

我也想知道如果您确实检索密码是否会有任何区别,因为数据库似乎存储了加盐密码的 SHA256 哈希。 哈希是不可逆的,因此即使您确实从数据库中检索了password列的内容,用户也无法使用它登录。

我认为在对任何网站的身份验证系统进行任何更改之前,您需要了解有关PHP编码以及安全性的更多信息。

这一行:

$stmt->bindValue( "password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR );

应该是:

$stmt->bindValue( "password", hash("sha256", $password . $salt), PDO::PARAM_STR );

不能在类定义之外使用$this->。时期。 'n . EOF .