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

How to get debug variable between try and catch in ci3 php

提问人:Zombatar 提问时间:8/21/2023 最后编辑:Mahesh ThoratZombatar 更新时间:8/23/2023 访问量:52

问:

我怎样才能得到一个$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";
}
php mysql ajax codeigniter try-catch

评论

0赞 ADyson 8/21/2023
Var_dump工作得很好,即使在 .您从脚本中收到的确切错误消息是什么?您是否尝试过任何其他调试?try

答:

0赞 Liam 8/22/2023 #1

看起来您正在测试一个 AJAX 请求(也称为 XHR),因此您应该在浏览器的开发工具“网络”选项卡中查找输出,过滤为“XHR”:Screenshot showing how to view XHR response

通过在以下代码行中回显某些内容来检查是否到达了该块:trytry

try {
    echo __LINE__;
    exit;

在代码中向下移动它,直到发现它没有被访问。这是找到有问题的线路的方法。如果抛出异常或错误,则块的其余部分将不会执行。这也许可以解释为什么看不到 的输出。echoexittryvar_dump

将块更改为 catch ,以便它可以捕获异常和错误。catchThrowable

通过回显 后面行上的内容来检查是否到达了该块。catchcatch

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