thisisedie Posted January 30, 2011 Share Posted January 30, 2011 Hello. First I'd like to say that I know my way around pretty good but I am not a veteran PHP programmer so talk to me like I'm stupid please Ok... I have a simple chat script set up that will only be used by a few people. I figured out how to truncate the chat when someone logs out, but I can't figure out how to truncate only if ALL users log out. Can someone lead me in the right direction? Thanks Link to comment https://forums.phpfreaks.com/topic/226140-truncating/ Share on other sites More sharing options...
ChemicalBliss Posted January 30, 2011 Share Posted January 30, 2011 Depending on how you store user activity this should be relatively simple. For a chat script a user can "logout" or "timeout", a timeout is when the user has no activity for say 5 minutes. Everytime a user sends a message or refreshes the page or basically interacts with the chat script there should be a timestamp in that users field, or a new entry with the userid and timestamp in a table like "active_users". Lets go with the active users method; User interacts with the site -> Timestamp inserted or updated in the active_users table. Whenever the script refreshes or someone requests content, it should check the "active_users" table for any timestamps that are more than 5minutes old. This is very simple as its just; if $mysql_user_timestamp + (60*5) < time() then delete the entry from the active users table. Similarly if the user manually logs out the active_users entry is removed for that user. That way you know if there are no entries in the active_users table then there is no one online and therefor you can truncate the chat. Is this what you are trying to do? Link to comment https://forums.phpfreaks.com/topic/226140-truncating/#findComment-1167420 Share on other sites More sharing options...
thisisedie Posted January 30, 2011 Author Share Posted January 30, 2011 Opps! I should have mentioned this uses a flat file to store messages. Link to comment https://forums.phpfreaks.com/topic/226140-truncating/#findComment-1167501 Share on other sites More sharing options...
ChemicalBliss Posted January 31, 2011 Share Posted January 31, 2011 How are you currently displaying the names in chat with flat file? are you using some sort of "active_users" file? Link to comment https://forums.phpfreaks.com/topic/226140-truncating/#findComment-1167588 Share on other sites More sharing options...
thisisedie Posted January 31, 2011 Author Share Posted January 31, 2011 No I dont have a list of users. Like I said its a very very simple script. Link to comment https://forums.phpfreaks.com/topic/226140-truncating/#findComment-1167600 Share on other sites More sharing options...
ChemicalBliss Posted January 31, 2011 Share Posted January 31, 2011 Do you even list the users that are in the chat? If not, i would suggest doing so by creating a seperate flat-file used to store usernames with a timestamp of their last activity on each line. Something like: someuser141::1296501723 This would be relatively easy to go through and put the users into an array which you can echo out. And also check their activity at the same time (for timeouts). To get a timestamp in php use the time() function. Once you get this working then you should work on truncating since you can then nkow how many people are in the chat. You could even save the chat to a log file once all the users have left. hope this helps Link to comment https://forums.phpfreaks.com/topic/226140-truncating/#findComment-1167953 Share on other sites More sharing options...
thisisedie Posted January 31, 2011 Author Share Posted January 31, 2011 Thanks so much for your help, I got it working Link to comment https://forums.phpfreaks.com/topic/226140-truncating/#findComment-1167959 Share on other sites More sharing options...
ChemicalBliss Posted January 31, 2011 Share Posted January 31, 2011 Congrats, I would highly reccommend database for scalability (adding to your current project). It will save a lot of effort . Also, since this problem is resolved at the bottom of this page is a "Mark Resolved" or similar button, feel free to click that Link to comment https://forums.phpfreaks.com/topic/226140-truncating/#findComment-1167961 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.