所以我有一个函数,可以调用ajax/从ajax返回。正如您所看到的,如果满足条件,我会在多个地方返回数据并停止脚本。这(显然)远不如我有一个函数那么简洁,我将错误消息传递给它,它返回/编码json,同时阻止主函数继续运行。问题是,考虑到函数中的函数没有被使用,我不知道如何在PHP中最好地构建这种东西。
抱歉代码太长了。。。我不知道该如何恰当地描述这个问题。
衷心感谢您的帮助(甚至是其他无关的提示)。非常感谢。
public function mask_as_user() {
$this->load->model('User_model', '', true);
$email = $this->input->post('email');
//there should be some front-end validation here too, but if not permitter, dont allow this to proceed.
if (strpos($this->session->userdata('user_type'), 'permitter') === false) {
$return['status'] = 'error';
$return['message']= 'You are not an admin.';
header('Content-type: application/json');
echo json_encode($return);
return false;
}
//save the current admin account, so we know where to switch back to
$admin_account_email = $this->session->userdata('user_email');
//logout current user to not have overlapping session data
//$this->logout(false, true);
$user_data = $this->User_model->get_user_data($email);
if ($user_data[0]){
$user_data = $this->clean_user_data($user_data[0]);
}
else{
$return['status'] = 'error';
$return['message']= 'This is not an active client account.';
header('Content-type: application/json');
echo json_encode($return);
return false;
}
//get userdata for user-mask account and remove unneccessary data (such as login credentials) from session array
//prevent switching into admin accounts. Not really any sensitive data that the rest of the company can't access elsewhere, but maybe someday there will be.
if (strpos($user_data['user_type'], 'permitter') !== false) {
$return['status'] = 'error';
$return['message']= 'You cannot switch into an admin account.';
header('Content-type: application/json');
echo json_encode($return);
return false;
}
//foreach column loaded from database, create a session value.
$this->session->set_userdata($user_data);
//set user to loggedin.
$this->session->set_userdata('loggedin', TRUE);
//set the current admin account which the mask is being applied to. We will need this for returning back to the admin account without having to logout.
$this->session->set_userdata('admin_account_email', $admin_account_email);
$return['status'] = 'success';
$return['redir_url'] = '/site_client/dashboard';
header('Content-type: application/json');
echo json_encode($return);
}
您可以使用try/catch语句,如下所示:
public function mask_as_user() {
$this->load->model('User_model', '', true);
$email = $this->input->post('email');
try {
//there should be some front-end validation here too, but if not permitter, dont allow this to proceed.
if (strpos($this->session->userdata('user_type'), 'permitter') === false) {
throw new Exception('You are not an admin.');
}
//save the current admin account, so we know where to switch back to
$admin_account_email = $this->session->userdata('user_email');
//logout current user to not have overlapping session data
//$this->logout(false, true);
$user_data = $this->User_model->get_user_data($email);
if ($user_data[0]){
$user_data = $this->clean_user_data($user_data[0]);
} else {
throw new Exception('This is not an active client account.');
}
//get userdata for user-mask account and remove unneccessary data (such as login credentials) from session array
//prevent switching into admin accounts. Not really any sensitive data that the rest of the company can't access elsewhere, but maybe someday there will be.
if (strpos($user_data['user_type'], 'permitter') !== false) {
throw new Exception('You cannot switch into an admin account.');
}
//foreach column loaded from database, create a session value.
$this->session->set_userdata($user_data);
//set user to loggedin.
$this->session->set_userdata('loggedin', TRUE);
//set the current admin account which the mask is being applied to. We will need this for returning back to the admin account without having to logout.
$this->session->set_userdata('admin_account_email', $admin_account_email);
$return['status'] = 'success';
$return['redir_url'] = '/site_client/dashboard';
header('Content-type: application/json');
echo json_encode($return);
return true;
}
catch (Exception $e) {
$return['message']= $e->getMessage();
$return['status'] = 'error';
header('Content-type: application/json');
echo json_encode($return);
return false;
}
}
我喜欢@user15的答案,这里是另一种构建数据然后返回数据的方法:
public function mask_as_user(){
$this->load->model('User_model', '', true);
$email = $this->input->post('email');
$data = array();
if(strpos($this->session->userdata('user_type'), 'permitter') === false){
$data['success'] = false;
$data['message']= 'You are not an admin.';
return $data;
}
$admin_account_email = $this->session->userdata('user_email');
$user_data = $this->User_model->get_user_data($email);
if (!$user_data[0]){
$data['success'] = false;
$data['message']= 'This is not an active client account.';
return $data;
}
$user_data = $this->clean_user_data($user_data[0]);
if (strpos($user_data['user_type'], 'permitter') !== false) {
$data['success'] = false;
$data['message']= 'You cannot switch into an admin account.';
return $data;
}
$this->session->set_userdata($user_data);
//set user to loggedin.
$this->session->set_userdata('loggedin', TRUE);
//set the current admin account which the mask is being applied to. We will need this for returning back to the admin account without having to logout.
$this->session->set_userdata('admin_account_email', $admin_account_email);
$data['success'] = true;
$data['redir_url'] = '/site_client/dashboard';
return $data;
}
这样称呼它:
header('Content-type: application/json');
$data = mask_as_user();
echo json_encode($data);
然后当您解析json时,检查状态bool $data['success']