【问题标题】:What is a solid approach to change passwords in PHP?在 PHP 中更改密码的可靠方法是什么?
【发布时间】:2015-12-29 19:09:30
【问题描述】:

我的脚本应该是这样工作的:

  • 输入旧密码并
  • 然后是新密码。

旧密码正在工作并已检查,但是当我插入新密码时,代码不起作用...没有错误,什么都没有...

这是我目前的代码:

$user_p = $_SESSION['user']['username'];

if(empty($_SESSION['user'])) 
{ 
    header("Location: live.php");      
    die("Redirecting to live.php"); 
} 

if(!empty($_POST)) 
{ 
    $currentPassword = preg_replace('/\s+/', '', $_POST['currentPassword']);
    $newPassword = preg_replace('/\s+/', '', $_POST['newPassword']); 
    $oldpass = IrBuscarPassword($_SESSION['user']['username']);
    $saltcode = IrBuscarSalt($_SESSION['user']['username']);

    $formEncriptedPass = hash('sha256', $currentPassword . $saltcode); 
        for($round = 0; $round < 65536; $round++) 
        { 
            $formEncriptedPass = hash('sha256', $formEncriptedPass . $saltcode); 
        } 

    $changepass = False;

    if($oldpass != $formEncriptedPass)
    {   
        echo "Password NO-OK.";
        //die();
    }
    else
    {

        if($newPassword == '')
        {
            $_SESSION['error'] = " The field E-mail is empty.</span></div>";
        }
        else
        {
            if($newPassword == '' || !isset($newPassword))
            {
                $changepass = False;

            } 
            else
            {

                $changepass = True;
                atualizarMail($newPassword, $_SESSION['user']['username']);
            }
        }

    }


    if(!isset($currentPassword) || ($currentPassword == ''))
    {
        $_SESSION['error'] = " The Password field is empty.</span></div>";
    }

    $password = hash('sha256', $_POST['currentPassword'] . $saltcode); 


    if($changepass == False)
    {
        $_SESSION['error'] = "<br/>New Password.</span></div>";
    }


    if($_POST['newPassword'] != $_SESSION['user']['username']) 
    { 


        $query = " 
            SELECT 
                1 
            FROM users 
            WHERE 
                password = :newPassword 
        "; 


        $query_params = array( 
            ':newPassword' => $_POST['newPassword'] 
        ); 

        try 
        { 

            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
        catch(PDOException $ex) 
        { 

            die("Failed to run query: " . $ex->getMessage()); 
        } 

        $row = $stmt->fetch();

    }

    if(!empty($_POST['newPassword'])) 
    { 
        $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647)); 
        $password = hash('sha256', $_POST['newPassword'] . $salt); 
        for($round = 0; $round < 65536; $round++) 
        { 
            $password = hash('sha256', $password . $salt); 
        } 
    } 
    else 
    { 
        $password = null; 
        $salt = null; 
    } 
        if(isset($_SESSION['error']))
        {
            echo $_SESSION['error']; 

            $_SESSION['error'] = null;
        }
    else
    {
        $_SESSION['user']['password'] = $_POST['newPassword']; 
        $_SESSION['success'] = " The password has been successfully changed..</span></div>";

        header("Location: password.php"); 

        die("Redirecting to logout.php"); 
    }
}

有人可以帮我吗?

【问题讨论】:

  • insert new password 在哪里?我在这里没有看到任何插入查询。
  • 您的代码中根本没有INSERT 语句...
  • 是的,但在 Function 中,不在那里..." -----> atualizarMail

标签: php hash passwords


【解决方案1】:

您的代码有很多问题,难以阅读/理解。一些例子:

  • 你一遍又一遍地检查同样的东西if($newPassword == '')
  • 你在一个函数中做不同的事情atualizarMail()
  • 您的哈希函数是不安全的,不能保证未来的发展。它至少在 3 个地方实施。储存盐会更容易。
  • 不应清理密码,仅验证密码(无 preg_replace()
  • if($_POST['newPassword'] != $_SESSION['user']['username']) 这行没有多大意义。
  • if语句层级太多,结合使用状态$changepass(难读,容易出错)
  • 查询 SELECT 1 FROM users WHERE password = :newPassword 希望永远不会获取任何数据,因为只有哈希存储在数据库中。

我希望我能指出,为什么我建议从头开始,在阅读了一个很好的教程之后。也许我可以给你一些开始宽度的想法:

// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_DEFAULT);

// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = password_verify($password, $existingHashFromDb);

第二个函数password_verify()可以用来登录,也可以用来检查旧密码是否匹配。

另一个提示,在脚本开始时验证所有输入并在有任何问题时立即重定向。验证后,不再检查无效输入,直接使用即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 2010-10-17
    • 2020-12-17
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    相关资源
    最近更新 更多