Search code examples
phpmysqlajaxcodeignitertry-catch

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


How can i get a $list_data_l?, when i click login it just appear error forbidden. I usually not using try catch on php, so the way i debug variable use var_dump, but on this case i cant get a debug variable use a variable. Is there any alternate solution to debug variable $list_data_l? i alreay use print_r, echo, and var_dump none of them works. please help

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";
}

Solution

  • It looks like you are testing an AJAX request (also know as XHR) so you should look for output in your browser's dev tools Network tab, filtered to "XHR": Screenshot showing how to view XHR response

    Check that the try block is being reached by echoing something on the line after try:

    try {
        echo __LINE__;
        exit;
    

    Move this echo and exit down through your code until you find that it is not being reached. This is how you can locate the line that has the problem. If there is an exception or error being thrown, then the rest of the try block will not execute. This might explain why you do not see the output of var_dump.

    Change the catch block to catch Throwable, so that it can catch both Exceptions and Errors.

    Check if the catch block is being reached by echoing something on the line after catch.

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