【问题标题】:PHP password change script via email通过电子邮件的 PHP 密码更改脚本
【发布时间】:2014-12-02 02:34:13
【问题描述】:

连接变量会在上面($dbConnected),但出于显而易见的原因,我把它们去掉了。至于这个问题,我似乎无法判断与我的数据库的连接是否存在问题,或者它是否是我的代码主体中的逻辑错误。

<?php

 $hostname = "";
 $username = "";
 $password = "";

 $databaseName = "";

 $dbConnected = mysql_connect($hostname, $username, $password);

 $dbSelected = mysql_select_db($databaseName, $dbConnected);

 if ($dbConnected) {
      $email = $_POST['email'];

      $query = mysql_query("SELECT * FROM Users WHERE Primary_Email = '$email'");   
      $numrows = mysql_num_rows($query);
        // Checking to see whether the email address is registered in the database
        if ($numrows == 1) {

        $pass = rand();
        $pass = md5($pass);
        $password = $pass;

        // Updating database with new password
        mysql_query("UPDATE Users SET UserPassword = '$password' WHERE User_Email = '$email'");

        $query = mysql_query("SELECT * FROM users WHERE User_Email = '$email' AND UserPassword = '$password'");
        $numrows = mysql_num_rows($query);
        if ($numrows == 1) {
          // Create email
          $webmaster = "admin@chaluparosa-gaming.com";
          $headers = "From: Ian Monson <$webmaster>";
          $subject = "Your new password";
          $message = "Hello. You have requested a password reset. Your new password is below. Please do not reply to this email, as it was automated \n
                      Password: $password \n ";

          if (mail($email, $subject, $message, $headers)) {
            echo "Your password has been reset. An email has been sent with your new password!"
            echo '<br />';

          } else {
            echo "Error in sending out the email...";
            echo '<br />';

          }

        }

    } else {
        echo "Email address was invalid or not found...!";
    }
} else {
  echo "Error connecting to the database...!";
}

?>

【问题讨论】:

标签: php database passwords


【解决方案1】:

您需要单独为 sql 添加错误消息,然后让其余代码显示自己的错误消息

 <?php

 $hostname = "";
 $username = "";
 $password = "";
 $databaseName = "";
 $dbConnected = mysql_connect($hostname, $username, $password) or die(mysql_error()); //die() with a mysql error message 
 $dbSelected = mysql_select_db($databaseName);
if (isset($email = $_POST['email'])) { //Check if email has been submitted
      $query = mysql_query("SELECT * FROM Users WHERE Primary_Email = '$email'") or die(mysql_error());   
      $numrows = mysql_num_rows($query);
        // Checking to see whether the email address is registered in the database
        if ($numrows == 1) {
        $pass = rand();
        $pass = md5($pass);
        $password = $pass;
        // Updating database with new password
        mysql_query("UPDATE Users SET UserPassword = '$password' WHERE User_Email = '$email'") or die(mysql_error());
        $query = mysql_query("SELECT * FROM users WHERE User_Email = '$email' AND UserPassword = '$password'") or die(mysql_error());
        $numrows = mysql_num_rows($query);
        if ($numrows == 1) {
          // Create email
          $webmaster = "admin@chaluparosa-gaming.com";
          $headers = "From: Ian Monson <$webmaster>";
          $subject = "Your new password";
          $message = "Hello. You have requested a password reset. Your new password is below. Please do not reply to this email, as it was automated \n
                      Password: $password \n ";
          if (mail($email, $subject, $message, $headers)) {
            echo "Your password has been reset. An email has been sent with your new password!"
            echo '<br />';
          } else {
            echo "Error in sending out the email...";
            echo '<br />';
          }
        }
    } else {
        echo "Email address was invalid or not found...!";
    }
}

【讨论】:

    猜你喜欢
    • 2012-06-24
    • 2014-01-16
    • 2015-02-14
    • 2015-06-10
    • 2015-04-17
    • 2014-06-17
    • 2015-03-06
    • 2011-04-09
    • 1970-01-01
    相关资源
    最近更新 更多