-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: Ajax request makes the session expire when app.sessionExpiration is set to 0 #5688
Comments
Can't reproduce in --- a/app/Config/App.php
+++ b/app/Config/App.php
@@ -174,7 +174,7 @@ class App extends BaseConfig
*
* @var int
*/
- public $sessionExpiration = 7200;
+ public $sessionExpiration = 0;
/**
* -------------------------------------------------------------------------- <?php
namespace App\Controllers;
class Home extends BaseController
{
public function index()
{
session()->set('user_id', 1);
return <<<'EOL'
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
var checkSession;
function CheckForSession() {
jQuery.ajax({
url: 'http://localhost:8080/home/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>
</head>
<body>
</body>
</html>
EOL;
}
public function checksession()
{
if (!session()->has('user_id')) {
$data = [
'sessionStatus' => 0
];
} else {
$data = [
'sessionStatus' => 1
];
}
return $this->response->setJSON($data);
}
} |
@kenjis can you try to use the .env file and set "app.sessionExpiration = 0" because only then it would expire i tried to set the sessionExpiration in /app/Config/App.php as you did and it worked fine, btw i am using the |
@rfikree92 Oh! It reproduced. The first Ajax request got:
But when I don't set
|
When I set |
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
and this is the controller/method
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();
tocookies([$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
The text was updated successfully, but these errors were encountered: