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

问题描述 投票:0回答:1
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";
        }
    }

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

php mysql ajax codeigniter try-catch
1个回答
0
投票

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

通过在

try
之后回显线路上的某些内容来检查是否已到达
try
块:

try {
    echo __LINE__;
    exit;

在代码中向下移动此

echo
exit
,直到发现无法到达它。您可以通过这种方式找到有问题的线路。如果抛出异常或错误,则
try
块的其余部分将不会执行。这可能可以解释为什么您看不到
var_dump
的输出。

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

通过在

catch
之后回显线路上的内容来检查是否到达
catch
块。

} catch (Throwable $e) {
    echo __LINE__;
    var-dump($e);
    exit;
© www.soinside.com 2019 - 2024. All rights reserved.