Hi guys i'm creating an online/offline system using PHP.
When user is logged in sessions are set and user is considered online and I run a set interval function that logs time(); into my database every 10 seconds.
setInterval(function(){
//update time every 10 seconds
$.get("timeupdate.php");
}, 10000);
I need some direction please in my next stage when I have to detect when the user is offline and I am slightly confused.
Do I run an if else statement?
$time = time();
$time2 = $row['time_update'] -> last updated time in my database
if($time > $time2 + 20) {
echo "user is offline";
}
because of the 10 second setinterval if $time ( the current unix timestamp) is greater than the last updated unix timestamp then user is offline.
am I right? and how would I go about implementing this display offline file?
Why implement the logic in PHP when the data in your database? If you're using MySQL date times....
SELECT last_seen_seconds_ago
, IF($MAXINTERVAL*1.05>last_seen_seconds_ago, 1, 0) AS status
FROM (
SELECT UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(time_update)
AS last_seen_seconds_ago
FROM yourtable
WHERE user='$user') ilv
(allows a 5% variance)
Related
I am building a task manager application where the user can start/stop a task. And I want to know when the user started it, when stopped it and how long task was running.
In the appcontroller.php I have set this timezone:
date_default_timezone_set("Europe/Athens");
But in taskscontroller.php when I save the time that the task has been started:
$now = FrozenTime::now();
$task->date_start = $now;
It is saved 2 hours ahead in the database (the field in the database is timestamp type).So when I click start at 9:00:00 I see that is saved as 11:00:00 in the database. Same when I click stop:
$now = FrozenTime::now();
$task->date_end = $now;
Again it is saved 2 hours ahead. But in my application when user clicks stop I want to calculate the time that the task was running. So I am calculating like this:
$task->total_minutes = ($now->diff($task->date_start))->format('%i'); //minutes
But even though date_start is : 2019-03-18 11:43:47
and date_end is : 2019-03-18 11:45:33
I get total_minutes: 58 which is obviously wrong... Why I get all these faulty behaviors?
I'm Italian, and i'm not good in English, but I Try to explain what i want to ask to you :)
I'd want to ask you a question about PHP and Sessions stored on the server.
I'm making a Play by chat online game using php and mysql.
What I need is to detect, using php, expired sessions for inactivity.
While an user is logging into the game, I update a table on a DB . This column that I update is the timestamp of the latest action done by the user. Then, i start a session for the client X. If this column is empty, it means that the user is logged off .
If an user do the logout correctly ( a button inside the game ), with php i destroy the session and i update the column of the DB with an empty timestamp, so the user results correctly logged off, but if there is a crash of the browser?
How can I detect that a session has been destroyed by the crash of the browser?
Thank you :)
you could store the connection ID in a seperate table, run a loop that periodically checks those connections for messages the client periodically would sent to denote activity
if the time difference is larger than the message update interval by a certain margin, you would drop the connection and/or update the tables
You have to set a "timeout", for example 10 minutes (600sec)
So, for any action of a user you check if he is current active by the difference of date_last_action and now().
For exampre.
Login : 2014-03-27 15:49:00
Last action: 2014-03-27 15:51:00 (only 2 minutes from the last action
[login])
Last action: 2014-03-27 16:21:00 (over time, so clear the db record,
delete his session)
$date_now = time();
$date= DATA VALUE OF LAST CURRENT USER ACTION;
$date1 = time();
$date2 = mktime($hh,$ii,$ss,$mm,$gg,$aaaa);
$dateDiff = $date1 - $date2;
$fullDays = floor($dateDiff/(60*60*24));
$fullHours = floor(($dateDiff-($fullDays*60*60*24))/(60*60));
$fullMinutes = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60);
$deadline_in_minutes=10;
if($fullMinutes>$deadline_in_minutes)
{
//do logout clear session
}
Ciao!
I would like to limit the access of a function i've created to once every 24 hour based on the users IP address.
I would also like the PHP script to delete the MySQL record if it's older than 24 hours.
If the user already has used the function within 24 hours, show them a message and prevent the script from continue running.
If the user already has used the function but 24 hours has passed since he used the function, delete the MySQL record and let the script continue running.
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$con=mysqli_connect("domain.com.mysql","domain_com","domain_password","domain_database");
$result = mysqli_query($con,"SELECT * FROM ipblock WHERE ip='".$ip."'");
while($row = mysqli_fetch_array($result));
if($ip == $row['ip']) //and code for checking how old the record is
{
// The user has used the function within 24 hours, kill the script.
echo "Come back in 24 hours";
exit;
}
else
{
// Looks clear, let them use the function
$MyFunction = true;
}
?>
I'm lost and as you can see i am also missing some statements for deleting old records (-24 hours)..
Could anyone provide me with an example of how to do this?
Thanks
Firstly store the IP and Access time as a pair in a table. Then you check is simple to see if there are any records in existence where the IP matches and the timestamp is less than 24 hours ago.
Before the function runs, complete an INSERT ... ON DUPLICATE UPDATE, and have the IP address as the primary key in the table.
The removal of old entries is then less of a priority and you can schedule this to be whenever convinient and remove entries where the access time is greater than 24 hours ago.
Store the access time as a myqsl date time, and do the comparison using the where clause:
WHERE accesstime >= DATE_SUB(NOW(), INTERVAL 1 DAY)
I'd create a table with function load
Store IP address and timestamp
When user loads the page, before running the function, select for IP adress and timestamp < 24 hours previous.
If you run the function: Store the users IP
No need to delete older records. But you could lik this to the verification function: delete all events older than 24 hours whenever the function is called for. Or set a cronjob to run every 12 hours to delete all older function load records
Basically, you can just create a timestamp field in the database. Then you compare using timediff() the stored values with the value of now().
I am working on a quite simple custom made content management system (CMS). There is a database table containing usernames, passwords, emailaddresses and a date/time field showing when the user logged in for the last time (I used "NOW()" in the query when updating).
When an user successfully logs in, the script will update a table in the database and sets the new time (using NOW()).
Now I would like to show which users are currently logged in, in the past 5 or 10 minutes. I have no idea how to accomplish this, therefore I am calling for your help. On the internet I read I need to do this with a timestamp, but I have no idea how this works.
I need to know how to set a specific timestamp.
I need to know how I can check in PHP if the user has been logged in in the past 5 or 10 minutes so I can display their names somewhere.
Thanks for your help!
1.
Why do you need to set a custom timestamp? Using NOW() is all you need.
2. This query will find all users where the column latest_login is in the past 10 minutes:
SELECT * FROM users WHERE latest_login >= DATE_SUB(NOW(), INTERVAL 10 MINUTE)
ok this is quite simple
1ts - convert your date into timestamp using function mktime (check google , lets say $date)
2nd - use function time() to get current timestamp (let say $now)
3rd if ( ($now - $date) > ( 5 * 60 /* 5 minute */ ) ) { return "not login" }
else return "login"
timestamp counts seconds so 5*60 is 5 minutes
I want to set up online detection on my website.
I have a row in my user table where the last login datetime is stored. Every time a user visits the site, his login date updates and user online row sets to 1 (1 - online, 0 - offline).
How to change the online row to 0 (offline) if the last login was 10 or more minutes ago? The aim is to find difference between dates.
cronjob every 10 minutes?
UDPATE users SET online = 0 WHERE login_date > (NOW() - INTERVAL 10 minute);
just to each user add a last_seen timestamp to there row so that when you do your user is authed check you can update the time
if(logged_in())
{
update_user();
}
function update_user()
{
//UPDATE users SET last_seen = unix_timestamp() WHERE uid = X;
}
Then you can do for you users:
SELECT * FROM users WHERE last_seen > (unix_timestamp()-300)
To get the last 5 mins.
If you want to show the users who have been online within last 10 mins then the best method is to include the datetime condition in the sql query.
Save the last login time as timestamp, then you can easily compare it with the current time and tell how much time has passed since.
Depending on the size of your user table, you can run the check of those who are still supposedly online every time somebody calls your website.
A different approach is to store active users in buckets, labeled with the last login time, you can then easily reset all users that are in buckets older than 10 minutes.