我是php
的初学者,如果用户在android
中请求,现在无法向用户电子邮件发送新密码。
忘记密码.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');
?>
这是我到目前为止尝试过的,不幸的是它给了我一堆错误!任何帮助将不胜感激。
我从这里获得教程。
错误(最新)
注意:未定义的索引:忘记密码 C:''xampp''htdocs''Android''CRUD''forgetPassword.php 在第 4 行 {"错误":2,"error_msg":"用户不存在"}
注意:未定义的索引:忘记密码
你必须确保 android 和 PHP 的索引相同。
将代码包装在以下检查周围:
if(isset($_POST['forgotpassword'])){//code here}else{//undefined}
警告:mysqli_query() 至少需要 2 个参数,给定 1
个
正如错误所说,缺少一个参数,即连接:
$result = mysqli_query($con,"SELECT email from users WHERE email = '$email'");
(将连接添加为 db 函数的第二个参数)
警告:mysqli_num_rows() 预期参数 1 mysqli_result, 空给定
这是因为查询 failed.to 避免此错误,请添加更多检查:
if($result){
$no_of_rows = mysqli_num_rows($result);
}else{
//failed
}