为了账号安全,请及时绑定邮箱和手机立即绑定

使用 php 在 pgadmin 上更改密码时出现问题

使用 php 在 pgadmin 上更改密码时出现问题

PHP
芜湖不芜 2023-03-04 16:21:58
我正在编写一个 PHP 文件以允许用户更改密码,但我遇到了一个奇怪的问题。我需要旧密码来确认帐户和新密码。鉴于凭据正确,此页面总是返回用户密码不正确,因此返回第 12 行“旧密码错误”中的回显。如果我在 pgAdmin 查询工具中启动“select * from utente”来查看密码,我在密码框中看不到任何变化。然后,如果我返回表单更改密码,如果我在旧密码框中输入我之前想更改的新密码,但似乎没有被接受,因为以前没有识别出旧密码,程序成功。我发誓我不明白为什么。我认为这是 md5 中的错误,但它也不适合 sha1。我知道两者都不安全,但现在我必须使用其中之一。我该如何解决?提前致谢<?php    $dbconn = pg_connect("host=localhost port=5432 dbname=progetto user=postgres password=password")    or die('Could not connect:' . pg_last_error());    if(!(isset($_POST['changeButton']))){        header("Location: utente.php");    }else{        $email = $_COOKIE["cookieEmail"];        $oldPassword = sha1($_POST['oldpassword']);        $q1="select * from utente where email = $1 and password = $2";        $result=pg_query_params($dbconn,$q1,array($email, $oldPassword));        if($line=pg_fetch_array($result ,null ,PGSQL_ASSOC)){            echo "<h1>Old password wrong</h1>            <a href=formCambiaPassword.php>Click here</a>";        }else{            $newPassword = sha1($_POST['newpassword']);            $q2 = "update utente set password=$1 where email=$2";            $result=pg_query_params($dbconn, $q2, array($newPassword, $email));            if($result==true){                $q3="select * from utente where email = $1 and password = $2";                $result=pg_query_params($dbconn,$q3,array($email, $newPassword));                if($line=pg_fetch_array($result ,null ,PGSQL_ASSOC)){                    echo "<h1>Error</h1>                    <a href=formCambiaPassword.php>Click here</a>";                }else{                    header("Location: utente.php");                }            }else{                echo "<h1>Error 2</h1>                        <a href=formCambiaPassword.php>Click here</a>";            }        }    }?>
查看完整描述

1 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

您的 if 语句在应该检查 false 时正在寻找 true。


if(!pg_fetch_array($result ,null ,PGSQL_ASSOC)){

您的代码应如下所示:


<?php

$dbconn = pg_connect("host=localhost port=5432 dbname=progetto user=postgres password=password")

or die('Could not connect:' . pg_last_error());

if(!(isset($_POST['changeButton']))){

    header("Location: utente.php");

}else{

    $email = $_COOKIE["cookieEmail"];

    $oldPassword = sha1($_POST['oldpassword']);

    $q1="select * from utente where email = $1 and password = $2";

    $result=pg_query_params($dbconn,$q1,array($email, $oldPassword));

    if(!pg_fetch_array($result ,null ,PGSQL_ASSOC)){

        echo "<h1>Old password wrong</h1>

        <a href=formCambiaPassword.php>Click here</a>";

    }else{

        $newPassword = sha1($_POST['newpassword']);

        $q2 = "update utente set password=$1 where email=$2";

        $result=pg_query_params($dbconn, $q2, array($newPassword, $email));

        if($result==true){

            $q3="select * from utente where email = $1 and password = $2";

            $result=pg_query_params($dbconn,$q3,array($email, $newPassword));

            if($line=pg_fetch_array($result ,null ,PGSQL_ASSOC)){

                echo "<h1>Error</h1>

                <a href=formCambiaPassword.php>Click here</a>";

            }else{

                header("Location: utente.php");

            }

        }else{

            echo "<h1>Error 2</h1>

                    <a href=formCambiaPassword.php>Click here</a>";

        }

    }

}

?>


查看完整回答
反对 回复 2023-03-04
  • 1 回答
  • 0 关注
  • 86 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信