Codeigniter throws 520 error CloudFlare while usin

2019-08-06 03:17发布

I am using Codeigniter with cloudflare and getting 520 error while storing user value in session during login.

Here is login function:

function check_login_submit($post_data) {
        if ($post_data) {

            $mob = trim($post_data['mob']);
            $password = trim($post_data['password']);

            $sql = "Select * from table where phone='$mob' and password='$password'";
            $query = $this->db->query($sql);
            $user = $query->row();

            if ($query->num_rows() == 1) {
            if ($user->status == 1)
            {


                $this->session->set_userdata('mem_id', $user->id);

                $this->session->set_userdata('mem_last_login_date', $user->last_login_date);

                $this->session->set_userdata('mem_created_on', $user->created_on);
                //-- Update last login of successfull Login
                $sql = "update table set last_login_date = NOW() where id=$user->id";
                $query = $this->db->query($sql);
                return TRUE;
            } 

                        }

            else {
                return FALSE;
            }

        }
    }

If i will stop the storing value into session user data than it will working fine however with session cloudflare give me 502 error page.

Please advise Thanks in advance for your time and support.

2条回答
再贱就再见
2楼-- · 2019-08-06 03:43

If anyone else runs into this problem, I came up with a solution that involves extending the core Session library that ultimately reduces the number of calls to sess_write() and by extension, _set_cookie().

MY_Session.php:

class MY_Session extends CI_Session {

    function set_userdata($newdata = array(), $newval = '', $write_session = true)
    {
        if (is_string($newdata))
        {
            $newdata = array($newdata => $newval);
        }

        if (count($newdata) > 0)
        {
            foreach ($newdata as $key => $val)
            {
                $this->userdata[$key] = $val;
            }
        }

        // Do not write the session (set the cookies) unless explicitly specified
        if ($write_session) {
            $this->sess_write();
        }
    }

    function set_flashdata($newdata = array(), $newval = '')
    {
        if (is_string($newdata))
        {
            $newdata = array($newdata => $newval);
        }

        if (count($newdata) > 0)
        {
            foreach ($newdata as $key => $val)
            {
                $flashdata_key = $this->flashdata_key.':new:'.$key;
                $this->set_userdata($flashdata_key, $val, false); // Do not update the cookie in the foreach
            }
        }

        // Save the cookie now that all userdata has been set
        $this->sess_write();
    }

    function _flashdata_mark()
    {
        $userdata = $this->all_userdata();
        $newUserData = array();
        $userDataToUnset = array();
        foreach ($userdata as $name => $value)
        {
            $parts = explode(':new:', $name);
            if (is_array($parts) && count($parts) === 2)
            {
                $new_name = $this->flashdata_key.':old:'.$parts[1];
                $newUserData[$new_name] = $value;
                $userDataToUnset[$name] = '';
                // Cookies were originally set in this loop. Moved to the end of the function
            }
        }

        // Save all changes outside of the loop
        if (count($newUserData) > 0) {
            $this->set_userdata($newUserData);
            $this->unset_userdata($userDataToUnset);
        }
    }
}
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-08-06 03:46

A 520 error generally indicates that there are large cookies or headers being returned that hit proxy buffer limits on our end. A HAR file send to our support team will help us figure out what the issue is.

查看更多
登录 后发表回答