Skip to content

Bug: Ajax request makes the session expire when app.sessionExpiration is set to 0 #5688

Description

@rfikree92

PHP Version

7.4

CodeIgniter4 Version

4.1.8

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

apache

Database

No response

What happened?

I have made an ajax script that keeps checking if the user session is valid or not and then it print to console if the session is expired

<script> 
      $(document).ready(function() {
        var checkSession;
        function CheckForSession() {
          jQuery.ajax({
            url: '<?= site_url('password/checksession'); ?>',
            headers: {'X-Requested-With': 'XMLHttpRequest'},
            type: "POST",
            cache: false,
            success: function(result) {
              if (result.sessionStatus == "0") {
                console.log('Your session has been expired!');
              }
            }
          });
        }
        checkSession = setInterval(CheckForSession, 5000);
      });
</script>

and this is the controller/method

public function checkSession()
    {
        if (!session()->has('user_id')) {
            $data = [
                'sessionStatus' => 0
            ];
        } else {
            $data = [
                'sessionStatus' => 1
            ];
        }
        return $this->response->setJSON($data);
    }

this script works only one time then in the browser console i see this warning message "Cookie “ci_session” has been rejected because it is already expired."

I tried to see whats causing this error and reached to this commit which changed how setCookie() method works, the old code works with my ajax requests but the new one doesn't, one thing i did was changing
cookies([$this->cookie], false)->dispatch(); to cookies([$this->cookie], true)->dispatch(); so it doesn't create a new instant of CookieStore and it worked again.

I am not sure why the author used "false" so i am not sure if my changes are proper or not.

Steps to Reproduce

I assume doing any ajax request to any controller should make the session expire

Note: app.sessionExpiration should be set to 0 for the error to be reproduced

Expected Output

session doesn't expire when doing ajax requests

Anything else?

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions