尝试通过php向电子邮件发送新密码时出错

时间:2016-01-28 17:44:39

标签: php android

我是php的初学者,如果用户在android中提出请求,现在却无法向用户电子邮件发送新密码。

ForgetPassword.php

  <?php 
require_once('DB_Functions.php');
    $db = new DB_Functions();
    $forgotpassword = $_POST['forgotpassword'];

    $randomcode = $db->random_string();
    if(isset($_POST['forgotpassword'])){
       $hash = $db->hashSSHA($randomcode);
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt = $hash["salt"];
        $subject = "Password Recovery";
        $message = "Hello User,nnYour Password is sucessfully changed. Your new Password is $randomcode . Login with your new Password and change it in the User Panel.nnRegards,nLearn2Crack Team.";
        $from = "contact@learn2crack.com";
        $headers = "From:" . $from;
     if ($db->isUserExisted($forgotpassword)) {

     $user = $db->forgotPassword($forgotpassword, $encrypted_password, $salt);
      if ($user) {
         $response["success"] = 1;
          mail($forgotpassword,$subject,$message,$headers);
         echo json_encode($response);
        }
        else {
       $response["error"] = 1;
      echo json_encode($response);
        }
     }

            // user is already existed - error response

        }
           else {

            $response["error"] = 2;
            $response["error_msg"] = "User not exist";
             echo json_encode($response);

}

?>

DB_Functions.php

    <?php
 require_once('dbConnect.php');
class DB_Functions {

    private $db;


    /**
     * Random string which is sent by mail to reset password
     */

public function random_string()
{
    $character_set_array = array();
    $character_set_array[] = array('count' => 7, 'characters' => 'abcdefghijklmnopqrstuvwxyz');
    $character_set_array[] = array('count' => 1, 'characters' => '0123456789');
    $temp_array = array();
    foreach ($character_set_array as $character_set) {
        for ($i = 0; $i < $character_set['count']; $i++) {
            $temp_array[] = $character_set['characters'][rand(0, strlen($character_set['characters']) - 1)];
        }
    }
    shuffle($temp_array);
    return implode('', $temp_array);
}

  public function isUserExisted($email) {
        $result = mysqli_query($this->db,"SELECT email from users WHERE email = '$email'");
        if($result)
        {
        $no_of_rows = mysqli_num_rows($result);
        if ($no_of_rows > 0) {
            // user existed
            return true;
        }
        }else {
            // user not existed
            return false;
        }
    }


 public function hashSSHA($password) {

        $salt = sha1(rand());
        $salt = substr($salt, 0, 10);
        $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
        $hash = array("salt" => $salt, "encrypted" => $encrypted);
        return $hash;
    }

public function forgotPassword($forgotpassword, $newpassword, $salt){
    $result = mysqli_query("UPDATE `users` SET 'password` = '$newpassword'
                          WHERE `email` = '$forgotpassword'");

if ($result) {

return true;

}
else
{
return false;
}
}
}
?>

dbConnect.php

<?php
    define('HOST','127.0.0.1:3307');
    define('USER','root');
    define('PASS','');
    define('DB','androiddb');

    //Connecting to Database
    $con= mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
?>

这是我到目前为止所尝试的,不幸的是它给了我一堆错误!任何帮助将不胜感激。

我从here获得了教程。

错误(最新)

  

注意:未定义的索引:忘记了密码   第4行的C:\ xampp \ htdocs \ Android \ CRUD \ forgetPassword.php   {&#34;错误&#34;:2,&#34; error_msg&#34;:&#34;用户不存在&#34;}

1 个答案:

答案 0 :(得分:1)

  

注意:未定义的索引:forgotpassword

你必须确保索引与android&amp; PHP。

围绕以下检查包裹您的代码:

if(isset($_POST['forgotpassword'])){//code here}else{//undefined}
  

警告:mysqli_query()需要至少2个参数,1个给定

如错误所示,缺少一个参数,即连接:

$result = mysqli_query($con,"SELECT email from users WHERE email = '$email'");

(将连接添加为数据库函数的第二个参数)

  

警告:mysqli_num_rows()期望参数1为mysqli_result,   null给出

这是因为查询失败。要避免此错误,请添加更多检查:

if($result){
   $no_of_rows = mysqli_num_rows($result);
}else{
   //failed
}