【问题标题】:change password in mysql table?更改mysql表中的密码?
【发布时间】:2013-01-24 21:05:53
【问题描述】:

您好,我的更改密码脚本有问题。我试图允许用户在 mysql 表 'ptb_users.password' 中更改他们的密码,假设将其存储为 md5。

当我在我的表单中点击提交时,我假设它转到了 changepassword.php 但页面只是空白,没有回显,我没有收到任何错误。

谁能告诉我我哪里出错了,谢谢

这是我的表格:

<?php 
// CONNECT TO THE DATABASE
    require('includes/_config/connection.php');
// LOAD FUNCTIONS
    require('includes/functions.php');
// GET IP ADDRESS
    $ip_address = $_SERVER['REMOTE_ADDR'];  
?>

  <?php require_once("includes/sessionframe.php"); 
  require('includes/checks.php');
?>


<?php

if (isset ($_GET['to'])) {
$user_to_id = $_GET['to'];

}

?> 

<?php 
//We check if the form has been sent
if(isset($_POST['subject'], $_POST['message_content']))
{
    $subject = $_POST['subject'];
    $content = $_POST['message_content'];
        //We remove slashes depending on the configuration
        if(get_magic_quotes_gpc())
        {
                $subject = stripslashes($subject);
                $content = stripslashes($content);
        }
        //We check if all the fields are filled
        if($_POST['subject']!='' and $_POST['message_content']!='')
        {
            $sql = "INSERT INTO ptb_messages (id, from_user_id, to_user_id, subject, content) VALUES (NULL, '".$_SESSION['user_id']."', '".$user_to_id."', '".$subject."', '".$content."');";
            mysql_query($sql, $connection);

            echo "<div class=\"infobox2\">The message has successfully been sent.</div>";
        }
}


if(!isset($_POST['subject'], $_POST['message_content']))

if (empty($_POST['subject'])){
        $errors[] = 'The subject cannot be empty.';

    if (empty($_POST['body'])){
        $errors[] = 'The body cannot be empty.';

    }
    }

{
?>


<form method="post" action="includes/changepassword.php" name="form1" id="form1">
<input type="password" name="oldpassword" id="password" class="subject" placeholder="Old Password">

<input type="password" name="oldpassword" id="password" class="message" placeholder="Old Password">

<input type="password" name="newpassword" id="newpassword" class="message" placeholder="New Password">

<input type="image" src="assets/img/icons/loginarrow1.png" name="submit" id="submit" class="submit">
</form>

这是我的 mysql 函数:

<?php
require_once("session.php"); 
require_once("functions.php");
require('_config/connection.php');
?>
<?php 

session_start();

include '_config/connection.php'; 

$password = $_POST['password'];
$newpassword = $_POST['newpassword'];
$confirmnewpassword = $_POST['confirmnewpassword'];

$result = mysql_query("SELECT password FROM ptb_users WHERE id=".$_SESSION['user_id']."");





if(!$result) 
{ 
echo "The username you entered does not exist"; 
} 
else 
if($password!= mysql_result($result, 0)) 
{ 
echo ""; 
} 
if($newpassword=$confirmnewpassword) 
{
    $newpassword=md5($newpassword);
    $sql=mysql_query("UPDATE ptb_users SET password='$newpassword' WHERE id=".$_SESSION['user_id'].""); 
}
    if($sql) 
    { 
    echo "Thank You. Your Password has been successfully changed."; 
    }
else
{ 
echo "The new password and confirm new password fields must be the same"; 
}  
?>

【问题讨论】:

  • 确保您的 php.ini 文件已开启错误报告和显示错误,您应该会开始看到一些错误消息。
  • 一个错误:if($newpassword=$confirmnewpassword) 必须是 if($newpassword==$confirmnewpassword)..请学习如何正确格式化代码..查看 psr-2 编码标准here
  • 首先,将die('this page shows up'); 放在&lt;?php 之后的第二行,让您真正到达该页面.. 让我知道..

标签: php html passwords


【解决方案1】:
if(isset($_POST['submit']))
{

   $email = $_POST['email'];
   echo $newpassword = ($_POST['password1']);
   echo $confirmpasssword = ($_POST['password2']);

        if($newpassword=$confirmpassword) 
        {
            echo $newpassword = md5($newpassword);
            echo $result = mysql_query("UPDATE users SET password='$newpassword' WHERE email='$email' "); 
        }
                if($result) 
                { 
                echo "Thank You. Your Password has been successfully changed."; 
                }
            else
            { 
            echo "The new password and confirm password fields must be the same"; 
            }  
}

can anyone tell me is this correct coding, to change password and store in mysqldb. 

【讨论】:

    【解决方案2】:

    首先您没有正确检查旧密码(存储 md5,明文比较...不起作用) 其次,您没有设置任何确认密码,所以这也行不通

    可行的方法是:

    $password = md5($_POST['password']);
    $newpassword = md5($_POST['newpassword']);
    
    $result = mysql_query("SELECT password FROM ptb_users WHERE id=".$_SESSION['user_id']." AND password = '".$password."'");
    if(!$result) 
    { 
    echo "The username you entered does not exist or old password didn't match"; 
    } 
    else
    {
         $sql=mysql_query("UPDATE ptb_users SET password='$newpassword' WHERE id=".$_SESSION['user_id'].""); 
    }
    if($sql) 
    { 
        echo "Thank You. Your Password has been successfully changed."; 
    }
    

    【讨论】:

    • 是的,这可以让用户更改他们的密码,但它不能正确确认旧密码,即使用户输入了错误的旧密码,它仍然允许用户更改它。在让用户更改旧密码之前,我怎样才能让它确认旧密码?
    • 不,当输入错误的“旧”密码时,它不会让您更改密码。 AND password = '".$password."'" 防止这种情况发生
    • 你试过运行代码吗,我只是说,因为当我运行代码时,它让我输入任何我想要的旧密码。
    • 它还允许用户将新密码框留空并将密码设置为空:/
    • 没有看到你检查任何东西;)代码和你的一样愚蠢,它只是在工作(不像你的)我的密码设置器完全不同,就我得到你想知道的问题而言为什么你没有得到任何回应..那是因为你总是在这行代码中结束echo ""; 我编辑了你的代码,这样它不仅可以工作,而且它也不再需要所说的行了......(a巧合)。如果您想为该任务提供完全不同的解决方案,那么您的问题具有误导性。
    【解决方案3】:

    这有很多问题。

    让我们先把基础知识弄清楚:

    1. 不要使用 mysql_ 函数。尽可能切换到 PDO 或 mysqli。

    2. md5 即将消亡。请参阅this 的回答 - 可以理解,您可能在 md5 中根深蒂固,如果不纠缠每个用户更新他们的密码,您就无法离开。

    那么你的问题是这样的:

    if($password!= mysql_result($result, 0))
    

    您不是在与存储的 md5 哈希值进行比较。应该是这样的:

    if(md5($password) != mysql_result($result, 0)) 
    

    还有这个:

    if($newpassword=$confirmnewpassword) 
    

    只是重新分配一个变量。我想你想要

    if($newpassword == $confirmnewpassword) 
    

    至于输出,您可能需要考虑您在此处使用的 if/else 结构。这可以显着清理,所有这些看起来都过时了。也许只是一个意见。

    如果您有具体的事情要磨练,请告诉我,我可能会更新。

    编辑

    应该清理整个块。这样的事情可能会有所帮助:

    if(!$result) 
    { 
        echo "The username you entered does not exist"; 
    } 
    else
    {
        if(md5($password) != mysql_result($result, 0)) 
        { 
            echo "Current PW does not match what we have"; 
        }
        else
        {
            if($newpassword == $confirmnewpassword) 
            {
                $newpassword=md5($newpassword);
                $sql=mysql_query("UPDATE ptb_users SET password='$newpassword' WHERE id=".$_SESSION['user_id']."") or die(mysql_error());
    
                if($sql) 
                { 
                  echo "Thank You. Your Password has been successfully changed."; 
                } 
            }
            else
            { 
                echo "The new password and confirm new password fields must be the same"; 
            }
        } 
    }
    

    【讨论】:

    • 新密码和确认新密码字段必须相同
    • 我稍微清理了一下。尝试在相关部分替换上面的代码
    猜你喜欢
    • 2021-06-27
    • 2013-09-06
    • 2018-06-23
    • 2014-07-17
    • 2021-05-04
    • 2014-12-10
    • 2012-02-06
    • 2015-12-14
    相关资源
    最近更新 更多