restful api 如何在 codeigniter 中更改密码?
How can do restful api change password in codeigniter?
如何在 Codeigniter 中使用 Restful API 更改密码功能。
我尝试了一个代码,但它不起作用,也没有在邮递员中捕捉到。
这是我的控制器部分:-
public function change_post()
{
try
{
$id = $this->input->post('id');
if ($id != '')
{
$this->load->library('form_validation');
$this->form_validation->set_rules('oldPassword','Old Password','required|max_length[20]');
$this->form_validation->set_rules('newPassword','New Password','required|max_length[20]');
$this->form_validation->set_rules('cNewPassword','Confirm new Password','required|matches[newPassword]|max_length[20]');
if($this->form_validation->run() == BOOL_F)
{
echo validation_errors();
}
else
{
$oldPassword = $this->input->post('oldPassword');
$newPassword = $this->input->post('newPassword');
$resultPas = $this->user_model->matchOldPassword($id, $oldPassword);
if($resultPas)
{
$this->response("password no match.", REST_Controller::HTTP_BAD_REQUEST);
}
else
{
$changeData = array('password'=>md5($newPassword));
$result = $this->user_model->changePassword($id, $changeData);
if($result > 0) { $this->response([
'message' => 'password changed successful.',
'data' => $change
], REST_Controller::HTTP_OK); }
else {$this->response("enter password first.", REST_Controller::HTTP_BAD_REQUEST);}
}
}
}
else
{
throw new Exception("The Data Already Register or The Data is Empty");
}
}
catch (Exception $e)
{
$error = array($e->getmessage());
$errormsg = json_encode($error);
echo $errormsg;
}
}
这是我的模型部分:-
function matchOldPassword($id, $oldPassword)
{
$this->db->select('id, password');
$this->db->where('id', $id);
$query = $this->db->get('admins');
$admin = $query->result();
if(!empty($admin)){
if(md5($oldPassword, $admin[0]->password)){
return $admin;
} else {
return array();
}
} else {
return array();
}
}
function changePassword($id, $adminData)
{
$this->db->where('id', $id);
$this->db->update('admins', $adminData);
$this->db->last_query();
return TRUE;
}
但它根本不起作用。我如何使用 Codeigniter
中的 Restful API 执行正确的方法来实施更改密码
这些是从该库到此应用程序的必需文件
application\libraries\REST_Controller.php
application\libraries\Format.php
application\config\rest.php
application\language\english\rest_controller_lang.php
需要更改
$change
未在您的代码中定义
控制器和模型做了细微的改动
这里是工作代码。
控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . '/libraries/REST_Controller.php';
use \Restserver\Libraries\REST_Controller;
class Test extends REST_Controller {
function __construct(){
parent::__construct();
$this->load->model('user_model');
}
public function index_post()
{
try{
$id = $this->input->post('id');
if ($id != '')
{
$this->load->library('form_validation');
$this->form_validation->set_rules('oldPassword','Old Password','required|max_length[20]');
$this->form_validation->set_rules('newPassword','New Password','required|max_length[20]');
$this->form_validation->set_rules('cNewPassword','Confirm new Password','required|matches[newPassword]|max_length[20]');
if($this->form_validation->run() == false)
{
echo validation_errors();
}
else
{
$oldPassword = $this->input->post('oldPassword');
$newPassword = $this->input->post('newPassword');
$resultPas = $this->user_model->matchOldPassword($id, $oldPassword);
if($resultPas)
{
$this->set_response("password no match.", REST_Controller::HTTP_BAD_REQUEST);
}
else
{
$changeData = array('password'=>md5($newPassword));
$result = $this->user_model->changePassword($id, $changeData);
if($result > 0) { $this->set_response([
'message' => 'password changed successful.',
'data' => $changeData
], REST_Controller::HTTP_OK); }
else { $this->set_response("enter password first.", REST_Controller::HTTP_BAD_REQUEST); }
}
}
}
else
{
throw new Exception("The Data Already Register or The Data is Empty");
}
}
catch (Exception $e)
{
$error = array($e->getmessage());
$errormsg = json_encode($error);
echo $errormsg;
}
}
}
?>
型号
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
function matchOldPassword($id, $oldPassword)
{
$this->db->select('id, password');
$this->db->where('id', $id);
$query = $this->db->get('admins');
$admin = $query->result();
if(!empty($admin) && (md5($oldPassword) == $admin[0]->password) ){
return false;
}
return true;
}
function changePassword($id, $adminData)
{
$this->db->where('id', $id);
$this->db->update('admins', $adminData);
$this->db->last_query();
return TRUE;
}
}
?>
如何访问
method - post
url - http://hostname/Test57471803/change
如何在 Codeigniter 中使用 Restful API 更改密码功能。
我尝试了一个代码,但它不起作用,也没有在邮递员中捕捉到。
这是我的控制器部分:-
public function change_post()
{
try
{
$id = $this->input->post('id');
if ($id != '')
{
$this->load->library('form_validation');
$this->form_validation->set_rules('oldPassword','Old Password','required|max_length[20]');
$this->form_validation->set_rules('newPassword','New Password','required|max_length[20]');
$this->form_validation->set_rules('cNewPassword','Confirm new Password','required|matches[newPassword]|max_length[20]');
if($this->form_validation->run() == BOOL_F)
{
echo validation_errors();
}
else
{
$oldPassword = $this->input->post('oldPassword');
$newPassword = $this->input->post('newPassword');
$resultPas = $this->user_model->matchOldPassword($id, $oldPassword);
if($resultPas)
{
$this->response("password no match.", REST_Controller::HTTP_BAD_REQUEST);
}
else
{
$changeData = array('password'=>md5($newPassword));
$result = $this->user_model->changePassword($id, $changeData);
if($result > 0) { $this->response([
'message' => 'password changed successful.',
'data' => $change
], REST_Controller::HTTP_OK); }
else {$this->response("enter password first.", REST_Controller::HTTP_BAD_REQUEST);}
}
}
}
else
{
throw new Exception("The Data Already Register or The Data is Empty");
}
}
catch (Exception $e)
{
$error = array($e->getmessage());
$errormsg = json_encode($error);
echo $errormsg;
}
}
这是我的模型部分:-
function matchOldPassword($id, $oldPassword)
{
$this->db->select('id, password');
$this->db->where('id', $id);
$query = $this->db->get('admins');
$admin = $query->result();
if(!empty($admin)){
if(md5($oldPassword, $admin[0]->password)){
return $admin;
} else {
return array();
}
} else {
return array();
}
}
function changePassword($id, $adminData)
{
$this->db->where('id', $id);
$this->db->update('admins', $adminData);
$this->db->last_query();
return TRUE;
}
但它根本不起作用。我如何使用 Codeigniter
中的 Restful API 执行正确的方法来实施更改密码这些是从该库到此应用程序的必需文件
application\libraries\REST_Controller.php
application\libraries\Format.php
application\config\rest.php
application\language\english\rest_controller_lang.php
需要更改
$change
未在您的代码中定义
控制器和模型做了细微的改动
这里是工作代码。
控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . '/libraries/REST_Controller.php';
use \Restserver\Libraries\REST_Controller;
class Test extends REST_Controller {
function __construct(){
parent::__construct();
$this->load->model('user_model');
}
public function index_post()
{
try{
$id = $this->input->post('id');
if ($id != '')
{
$this->load->library('form_validation');
$this->form_validation->set_rules('oldPassword','Old Password','required|max_length[20]');
$this->form_validation->set_rules('newPassword','New Password','required|max_length[20]');
$this->form_validation->set_rules('cNewPassword','Confirm new Password','required|matches[newPassword]|max_length[20]');
if($this->form_validation->run() == false)
{
echo validation_errors();
}
else
{
$oldPassword = $this->input->post('oldPassword');
$newPassword = $this->input->post('newPassword');
$resultPas = $this->user_model->matchOldPassword($id, $oldPassword);
if($resultPas)
{
$this->set_response("password no match.", REST_Controller::HTTP_BAD_REQUEST);
}
else
{
$changeData = array('password'=>md5($newPassword));
$result = $this->user_model->changePassword($id, $changeData);
if($result > 0) { $this->set_response([
'message' => 'password changed successful.',
'data' => $changeData
], REST_Controller::HTTP_OK); }
else { $this->set_response("enter password first.", REST_Controller::HTTP_BAD_REQUEST); }
}
}
}
else
{
throw new Exception("The Data Already Register or The Data is Empty");
}
}
catch (Exception $e)
{
$error = array($e->getmessage());
$errormsg = json_encode($error);
echo $errormsg;
}
}
}
?>
型号
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
function matchOldPassword($id, $oldPassword)
{
$this->db->select('id, password');
$this->db->where('id', $id);
$query = $this->db->get('admins');
$admin = $query->result();
if(!empty($admin) && (md5($oldPassword) == $admin[0]->password) ){
return false;
}
return true;
}
function changePassword($id, $adminData)
{
$this->db->where('id', $id);
$this->db->update('admins', $adminData);
$this->db->last_query();
return TRUE;
}
}
?>
如何访问
method - post
url - http://hostname/Test57471803/change