Skip to content
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

Bots (friendly) executes the session class and creates new db rows every second #7

Open
jonasbirkelof opened this issue Oct 21, 2020 · 1 comment

Comments

@jonasbirkelof
Copy link

My IT department are running a monitoring system that checks for errors etc, and that script runs every second and generates a new session class initialization, which in turn creates a new row in the database. That's one new row every second!

My IT department can't do much about this on their end. Do you have an idea on how I can attack this problem? I think this happens since the class constructor executes all functions and I need to initiate the class on my index.php to check if a user is logged in or not, and that's the page the monitoring system looks at. Can empty rows (with empty data column) be deleted automatically or can a new row only be created when I run my login script?

On my localhost I don't have this issue. Otherwise the code works great! :)

@jonasbirkelof
Copy link
Author

jonasbirkelof commented Oct 21, 2020

I solved this by adding this code at the end of the constructor:

$this->_gc(10, 180)

and this in the _gc($max, $maxLoggedIn) method:

$old = time() - $max;
$oldLoggedIn = time() - $maxLoggedIn;

$this->db->query('DELETE FROM sessions WHERE (access < :old AND data = "") OR (access < :oldLoggedIn)');

$this->db->bind(':old', $old);
$this->db->bind(':oldLoggedIn', $oldLoggedIn);

$max = if a visitor does not set a session, their row will be deleted, until they set a session

$MaxLoggedIn = if a logged in user (in this case) has data in the data cell in the db and has been inactive (not updated the page) for this amount of seconds, their row will be deleted. In this case logged out.

This is might only be needed in my specific case, because of the monitoring script, but this provides some control of old data and a way to set a max session time for your users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant