codeigniter 如何在ci3 php中获取try和catch之间的调试变量

ippsafx7  于 8个月前  发布在  PHP
关注(0)|答案(1)|浏览(76)

如何获取$list_data_l?,当我点击登录它只是出现错误禁止.我通常不会在php上使用try catch,所以我调试变量的方式是使用var_dump,但在这种情况下,我不能使用变量来获得调试变量。是否有调试变量$list_data_l的替代解决方案?我已经使用了print_r,echo和var_dump,它们都不起作用。请帮

if ($this->input->is_ajax_request()) {
    try {
        $this->form_validation->set_rules('userid', 'ID', 'required|xss_clean');
        $this->form_validation->set_rules('pass', 'Password', 'required|xss_clean');
        if ($this->form_validation->run() == FALSE)
            throw new Exception(validation_errors("", ""), 0);

        date_default_timezone_set("Asia/Jakarta");
        $current_date_time = date("Y-m-d H:i:s");

        $this->two_db = $this->load->database('database_two', TRUE);

        $userid = $this->input->post("userid");
        $pass = $this->input->post("pass");
        $pass1 = md5($pass . "monda");
        $sql1 = "SELECT A.id, A.userid,A.`name`,A.`active_flag`,A.`group`
                        FROM login_session A
                        WHERE A.`userid`=? AND A.`password`=? ";
        $bind1 = array($userid, md5($pass . "monda"));
        $list_data_l = $this->two_db->query($sql1, $bind1);

        var_dump($list_data_l);
        die;

        if (!$list_data_l)
            throw new Exception("SQL 2 Error!");
        if ($list_data_l->num_rows() == 0)
            throw new Exception("Id Login atau Password salah!");

        $sql = "SELECT A.id, A.userid,A.`name`,A.`active_flag`,A.`groupid`,B.`name` groupname,A.unit_code
                        FROM tbl_user A
                        INNER JOIN `tbl_user_group` B ON A.`groupid`=B.`id`
                        WHERE A.`userid`=? ";

        $bind = array($userid);
        $list_data = $this->db->query($sql, $bind);
        if (!$list_data)
            throw new Exception("SQL Error!");
        if ($list_data->num_rows() == 0)
            throw new Exception("Wrong Combination!");
        if ($list_data->row()->active_flag != 'Y')
            throw new Exception("Your account is blocked!");
        $this->session->set_userdata('peppd', $list_data->row());
        //update last access
        $this->db->trans_begin();
        $this->m_ref->setTableName("tbl_user");
        $data_baru = array(
            "last_access" => $current_date_time,
        );
        $cond = array(
            "id" => $list_data->row()->id,
        );
        $status_save = $this->m_ref->update($cond, $data_baru);
        if (!$status_save) {
            throw new Exception($this->db->error("code") . " : Failed save data", 0);
        }

        $this->db->trans_commit();
        $output = array(
            "status" => 1,
            "msg" => "You logged in",
            "csrf_hash" => $this->security->get_csrf_hash(),
        );
        exit(json_encode($output));
    } catch (Exception $e) {
        $this->db->trans_rollback();
        $this->load->helper('captcha');
        $original_string = array_merge(range(1, 9), range('A', 'Z'));
        $original_string = implode("", $original_string);
        $captcha = substr(str_shuffle($original_string), 0, 5);
        $vals = array(
            'word' => $captcha,
            'img_path' => './captcha/',
            'font_path' => './fonts/KeepCalm-Medium.ttf',
            'img_url' => base_url("captcha"),
            'img_width' => 200,
            'img_height' => 30,
            'expiration' => 7200,
            'word_length' => 5,
            'font_size' => 15,
            'pool' => '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',

            // White background and border, black text and red grid
            'colors' => array(
                'background' => array(255, 255, 255),
                'border' => array(255, 255, 255),
                'text' => array(0, 0, 0),
                'grid' => array(255, 73, 142)
            )
        );

        $cap = create_captcha($vals);
        //        print_r($cap);exit();
        $this->session->set_userdata("captchaword", $cap["word"]);
        $output = array(
            'status' => 0,
            "msg" => $e->getMessage(),
            "captcha_img" => $cap["image"],
            "csrf_hash" => $this->security->get_csrf_hash(),
        );
        exit(json_encode($output));
    }
} else {
    echo "denied";
}
wn9m85ua

wn9m85ua1#

看起来你正在测试一个 AJAX 请求(也称为XHR),所以你应该在浏览器的开发工具网络选项卡中查找输出,过滤为“XHR”:

通过回显try之后的行上的内容来检查是否到达了try块:

try {
    echo __LINE__;
    exit;

在代码中向下移动echoexit,直到发现没有到达。这是您找到有问题的线路的方法。如果抛出异常或错误,则try块的其余部分将不会执行。这也许可以解释为什么看不到var_dump的输出。
catch块更改为捕获Throwable,以便它可以捕获异常和错误。
通过回显catch之后的行上的内容来检查是否到达了catch块。

} catch (Throwable $e) {
    echo __LINE__;
    var-dump($e);
    exit;

相关问题