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.
Related
I'm new to PHP and I'm hoping to make a script where a user will get a "coin" every hour that they go on the page. For instance, if a user logs in twice during the same hour, they will only get one coin. But if they refresh the page during the next hour, they get another coin. However, they do not get coins when they do not refresh the page, even if many hours go by.
How would I even start going about doing this? Any help would be extremely appreciated.
Easy... :) If you are using MySQL or something to store the coins, get the time too, when the coin was credited. And each time the page is called, check the time. A pseudo code would be like this:
load(coins);
timeDiff = timeNow - timeLastCredited;
if (timeDiff > 1 hour)
coins++;
save(coins);
In case of PHP, I guess you may do like this:
$coins = getCoins(); // Assuming this function will load the current coins count from DB.
$lastCredit = getLastCoinCreditedTime(); // Should return a DateTime integer.
$timeDiff = microtime() - strtotime($lastCredit);
if ($timeDiff > 60*60*60*1000)
saveCoins($coins+1); // Assuming this function saves the new number of coins.
I'd do it this way:
On each page load (refresh, login, whatever), check to see if the user has already received a coin for the current hour. To do this, you need to know what the current hour is:
$hour = (new DateTime())->format("Y-m-d-h");
This will give a value like "2013-03-25-11" during the 11:00 hour. I'm including the date, since we don't to want skip giving a coin in, say, the 11 o'clock hour just because they were online yesterday at 11:05.
Then you can either:
Add one to their coin total whenever you have a new $hour value (ie. it's not recorded in your database) and save the $hour value, or
Save the $hour value in the database and count the total number of coins earned with a query like SELECT COUNT(DISTINCT hour) FROM table;
The first approach is useful if they're spending the coins on something (ie. you can add/subtract from their "coin account"); the second is useful if you just want a grand total of the number of coins (ie. the total number earned, ever).
I'm going to assume that you have a users table in a database.
If that is the case, you can use a column in the database to update the latest time that a user has gone on the page with php and a query on that page. Then you can set up a cron job that runs every hour that will query the users table and see if the user has viewed the page within the last hour. If the user has viewed the page within the last hour, increment the amount of coins associated with that user.
I have a script that will login and logout a user. It works perfectly. Now I have like a widget that counts how many users are registered and activated as well as how many users are online. I do this by having a field in my users database that says online = 1 or 0. When the person logs in, online = 1 and logs out online = 0. Now I haven't taken into account that this field is only being updated because the user is doing something. I haven't taken into account that the session would timeout.
How can I make a function that says something like if session timeout = true then update users set online=0 where username=$username and user_id=$user_id.
In your database table, add another column something like last_seen. Update this every time you see your users online. After a certain period of inactivity, they will be marked as inactive. In fact, I suggest you replace your online field with this.
For example,
ALTER TABLE users CHANGE COLUMN `online` `online` DATETIME; -- SAMPLE SQL query only
To check how many users are online:
SELECT * FROM users WHERE online>(NOW()-INTERVAL 1 HOUR); -- SELECTS all users online in the past hour.
If the user logs out, you can simply set the online = NOW()-INTERVAL 1 HOUR. Or, you can also retain your previous online field and you can check if the user is idle (using my suggestion) OR online=0.
Instead of trying to use a boolean value to see if somebody is logged in, try using a TIMESTAMP. Then you can perform more accurate logic based on how long somebody has been away. If the last time somebody has loaded a page on your website was 30 minutes ago, do you think they're online? Do you even think they're at their keyboard?
The session will only timeout if you want it to do so. This question is really a duplicate of 'How do I expire a PHP session?'
Code is only executed when a php page is served, so you will need to track the last time a user was active my using a session variable to track the last time a page was served to that user. Then, whenever serving any php page to the user, check to see if the timeout period has elapsed and log the user out if it has, see link for examples.
Add lastOnline field which stores the timestamp of last user activity.
Have some ajax function on the page which updates the timestamp every "n" seconds.
To check if user is online - check both: online field and timestamp. If timestamp was updates more than "n" seconds ago - user is offline even if online field is equal 1.
i am new in php.. i want to delete the user automatically if he does not attempt login in 30 days.
for example
if user login on "10-02-2012" ,
and if user doesnot login for next 30 days
then system should automatically delete his account.
if user again login on "15-02-2012" ,
then limit should be for next 30 days i-e "15-03-2012"
please help me i am very new in php
i have no idea how to store the date when user attempt to login.
You want to have a date field in the user table and a query that sets that date to CURDATE() whenever your login script runs. Something like:
UPDATE 'users' SET 'lastlogin' = CURDATE() WHERE 'userid' = '$userid';
Have a crontab that runs once a day (or however often you want) that queries all the fields that are 31 days old and deletes them:
DELETE FROM 'users' WHERE 'lastlogin' < CURDATE() - INTERVAL 31 DAY
Log the last login date in a Database.
Write a script which searches and deletes users where the last login was more then 30 days ago.
start the search and delete script with a cron job
Here's a tutorial on building a login system:
http://www.phpeasystep.com/phptu/6.html
Your solution would add to the tutorial by adding a DATETIME field named "last_login" to the members table. Whenever someone logs in, you update the last_login field with a database query like:
UPDATE TABLE members SET last_login = CURRENT_TIME WHERE id = xxx LIMIT 1
Then you can run another database query once a day to delete inactive accounts, customizing the deletion date as needed:
DELETE FROM TABLE members WHERE last_login < '2012-04-01 00:00:00'
it's very simple.
create a field in the DB table that stores the most recent login date.
write a script run every night at midnight that checks the login date against the current date.
the great thing about this is date objects allow you to easily compare dates easily.
here are some links that will help:
http://php.net/manual/en/function.date.php
http://www.w3schools.com/sql/sql_dates.asp
best of luck! it's pretty straight forward I have done it many times im sure you wont have much trouble.
In your data base, where user accounts are stored, you can store the last time they logged in using one of the built in MySQL date/time data types. In your PHP you can update this to the current time with another MySQL command. This page will get you started: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
As for deleting a user, you will want to set up a cron job/scheduled task, that will delete this user by checking the date and seeing if it is 30 days ago.
You can add two field for users table
created : date when user registered
login_date: date update on every login by user.
And you can use cron Job which run automatically (run a php file which path set in it) on selected time. you can run it every day on a fixed time. and if found both created and login_date same then delete that user from database. You can set cron job from your cpanel.
thanks
set up cron job for once a day and check who didn't logged in from last 30 days to current time / date you can use timestamp or date for last login
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'm working on a "community". And of course I would like to be able to tell if a user is online or offline.
I've created so that when you log in a row in my table UPDATE's to 1 (default is 0) and then they're online. And when they log out they're offline. But if they don't press the Log out button, they will be online until they press that button.
So what I would like to create is:
After 5 minutes of inactivity the row in my database should UPDATE to 0.
What I'm looking for is how to do this the easiest way. Should I make an mysql_query which UPDATE's the row to 1 every time a page is loaded. Or is there another way to do it?
Instead of using a boolean "Online" field, use a DateTime. When a user makes a request to the page, update the DateTime to NOW(). When you are gathering your list of current users online, your WHERE clause would be something like WHERE lastSeen > DATE_SUB(NOW(), INTERVAL 5 Minutes)
Update: To retrieve individual online status.
select if(lastSeen > date_sub(now(), interval 15 minutes), 1, 0) as status from table where userid=$userid
This tutorial is quite handy: Who Is Online Widget With PHP, MySQL & jQuery
Well, if you don't want to set up a cron job, that would execute some code every 5 minutes, you have no options. But, actually, I think the following approach would be much more efficient:
Change your 1/0 column to timestamp
On each user request update that timestamp to current DateTime.
When checking for active users, check if that timestamp is less than 5 minutes from now
This way you'll be having actual data on users and no recurring queries - just one additional update per request
If you will update the row only on page load, then some of information would be incorrect.
Let's assume that user have opened page and is writing really long text or something. He is doing it for half an hour now. And your database ny now is already updated and he is counted as offline user.
I would write javascript that pings you back each 5 minutes, if opened tab is active.
This ping updates database field 'last_activity' to NOW(). And to count online users, or check if user is online you'll need to compare 'last_activity' to NOW() minus five minutes.
Simpliest ways (IMHO):
You can count sessions in session_save_path() dir.
you can store last visit timestamp in DB, and count rows with (timestamp > current_timestamp - somedelay).