Reputation: 361
I was just wondering is there any way to store a bit of php script separate from the actual webpage that runs constantly on the server.
For example I have this MySql query; $sql = "UPDATE users SET connection= IF(NOW() - last_access_time > 3, 0, 1)";
basically it checks every user on the database which are currently using the website. The requirement of the website force me to do this every 2 seconds on almost every page.
it seems like it creates a lot of traffic on the server side. So is there any way to run this query separate from the website? Or is there any other way to solve this problem in general?
Upvotes: 2
Views: 94
Reputation: 2856
Not sure how to abstract out this particular case, but it looks to me like you're trying to maintain a record in your database of whether the user is currently active on the site.
How about this instead?
On every page run this:
$sql = "UPDATE users SET last_access_time = NOW() WHERE id=".$_SESSION['USERID'];
Then in your scripts where you are looking for users with a connection do it this way:
$sql = "SELECT * FROM users WHERE last_access_time < NOW()-3";
The way you're running it now, your db server has to run through every user everytime someone hits a page, to set the status to active or not. Instead you could only hit the db everytime you actually need that information.
Upvotes: 1