所以我有一个可以调用/返回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;
}
}
关于php - php 中函数内函数的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31173110/