使用WordPress User Credentials和php脚本将数据发送到iOS应用程序

所以我在过去两天一直在努力工作,试图做这个工作,但是我总是被困在相同的部分:将从iOS应用程序接收到的计算机密码与存储在Wordpress数据库中的哈希密码进行比较。

一个小背景:基本上只是一个ios故事板上的login页面,发送用户input的电子邮件和密码到一个php脚本,检查链接到发送的电子邮件的哈希密码是否与发送密码的散列版本相同。

Swift和iOS不是这里的问题,所以我会把你的代码放在这里。 我正在做我的testing使用PostMan发送HTTP POST请求与正确的参数:

  • Header – > Content-Type:application / x-www-form-urlencoded
  • 身体 – >电子邮件:email@email.com和密码:testpasswd

现在这里是login页面的原始代码:

<?php require("Connection.php"); require("SQLDao.php"); $email = htmlentities($_POST["email"]); $password = htmlentities($_POST["password"]); $returnValue = array(); echo json_encode("Hello World"); if(empty($email) || empty($password)) { $returnValue["status"] = "error"; $returnValue["message"] = "There is a missing field"; echo json_encode($returnValue); return; } $dao = new MySQLDao(); $dao->openConnection(); $userDetails = $dao->getUserDetailsWithPassword($email,$password); if($userDetails) { $returnValue["status"] = "Success"; $returnValue["message"] = "User logged in !"; echo json_encode($returnValue); } else { $returnValue["status"] = "error"; $returnValue["message"] = "User not found"; echo json_encode($returnValue); } $dao->closeConnection(); ?> 

大部分SQLDao.php基本上处理SQL连接function,但也是这个function:

 public function getUserDetailsWithPassword($email, $userPassword) { $returnValue = array(); $sql = "select id,user_email from vswp_users where user_email='" . $email . "' and user_pass='" .$userPassword . "'"; $result = $this->conn->query($sql); if ($result != null && (mysqli_num_rows($result) >= 1)) { $row = $result->fetch_array(MYSQLI_ASSOC); if (!empty($row)) { $returnValue = $row; } } return $returnValue; } 

但是,这段代码显然会检查发送给WordPress的散列版本的密码,所以它总是返回false。

通过我的研究,我发现了这一点,但没有给我一个工作的解决scheme:

研究没有。 1:include(phpass.php)+ CheckPassword函数在这里我会有类似的东西:

 public function getUserDetailsWithHashedPassword($email, $userPassword) { include_once("../wp-config.php"); include_once("../wp-includes/class-phpass.php"); $returnValue = array(); $query = mysql_query("SELECT * FROM vswp_users WHERE user_email = '$email'"); $row = mysql_fetch_array($query); $wp_hasher = new PasswordHash(8, TRUE); $password_hashed = $row['user_pass']; $passwordMatch = $wp_hasher->CheckPassword($userPassword, $password_hashed) || $password_hashed == md5($userPassword); if ($passwordMatch) { echo json_encode("Passwords match"); $returnValue = TRUE; } else { echo json_encode("Passwords do not match"); $returnValue = FALSE; } return $passwordMatch; } 

但是这只是返回“Hello World”,但是没有显示if($ passwordMatch)的选项。

我对这个长期的问题表示歉意,希望有人能指出我正确的方向来解决这个问题。

谢谢你的帮助 !!

编辑1:通过一些testing使用echo json_encode("Hello World"); 我发现脚本基本上停止了 – > ERASED Since US NON-RELEVANT ANYMORE

编辑2:我越来越接近,感谢CBroe的想法,我能够find更多的信息与适当的PHP报告。 我能够通过将存储在数据库中的哈希密码直接存储在login文件中作为$storedPassword的值来工作。 但是现在唯一的问题是我在使用mysql的时候遇到以下错误: substr() expects parameter 1 to be a string, array given in /[...]/class-phpass.php 。 看来我只是缺less一些东西来将密码从mysql数据库转换为string版本。 这是我的function:

 public function getUserDetailsWithHashedPassword($email, $userPassword) { require_once("/home/[hosting provider directories]/public_html/wp-includes/class-phpass.php"); $returnValue = array(); $sql = "select user_pass from vswp_users where user_email='" . $email . "'"; $result = $this->conn->query($sql); if ($result != null && (mysqli_num_rows($result) >= 1)) { $row = $result->fetch_array(MYSQLI_ASSOC); if (!empty($row)) { $storedPassword = $row; } } $wp_hasher = new PasswordHash(8, TRUE); $passwordMatch = $wp_hasher->CheckPassword($userPassword, $storedPassword); if($passwordMatch === TRUE) { $returnValue = TRUE; } else { $returnValue = FALSE; } return $returnValue; } 

再次感谢 !!

首先从数据库中获取用户详细信息。 然后用明文密码检查用户的散列密码。

要获取用户,您可以尝试:

 $user = get_user_by( 'email', $email ); 

然后:

 if ( $user && wp_check_password( $userPassword, $user->data->user_pass, $user->ID) ){ echo json_encode("Passwords match"); $returnValue = TRUE; } else{ echo json_encode("Passwords do not match"); $returnValue = FALSE; } 

相关参考文献

wp_check_password

get_user_by

我终于能够使它工作。 我的最后一个问题与$row是我没有指定我想显示哪个数组键。 这里是工作代码,如果有人在这里结束,需要工作的解决scheme! (显然[托pipe服务提供商目录]只是我的内部文件系统,如果需要将其更改为你的)

 public function getUserDetailsWithHashedPassword($email, $userPassword) { require_once("/home/[hosting provider directories]/public_html/wp-includes/class-phpass.php"); $returnValue = array(); $sql = "select user_pass from vswp_users where user_email='" . $email . "'"; $result = $this->conn->query($sql); if ($result != null && (mysqli_num_rows($result) >= 1)) { $row = $result->fetch_array(MYSQLI_ASSOC); if (!empty($row)) { $storedPassword = $row['user_pass']; } } $wp_hasher = new PasswordHash(8, TRUE); $passwordMatch = $wp_hasher->CheckPassword($userPassword, $storedPassword); if($passwordMatch === TRUE) { $returnValue = TRUE; } else { $returnValue = FALSE; } return $returnValue; }