I have created a chatroom.php and i made target="_blank" that directs the user to the appropriate room whenever a user clicks a room's link. Also, when a user clicks that link i stores the nickname of the user in database, to display the nicknames of the users in that room. However, when a user closes a room's window it still displays that user is online which is not wanted. I need a way that whenever a user closes a room such as chatroom?name=book, i will delete him/her from that room. I hope i made myself clear. It should work like logout.php for certain room. How can i do that?
Thanks
Have a look at the onBeforeClose event.
window.onbeforeclose = function(){
// make a call to PHP saying user is singing off.
};
Alternatively you can use a "idle" timeout (if user hasn't said anything in X minutes, auto-log them off.)
To do this,
You need to create a Automatic Task to check for the users which are still their in the CHAT ROOM using SESSION_Checking or ACTIVITY_Monitoring.
Once user closes window, The TASK will check and make necessary changes.
To implement in PHP using SESSION Checking:
Every time a user logs in you set $_SESSION['online'] = 1; - the session variable is set only for this user so you don't have to worry for conflicts. But if you want to display this info to all users you should probably save the last action time of your user to the DB. if($_SESSION['online'] == 1;) mysql_query('UPDATE user SET last_activity=NOW() WHERE id='.$user_id); or like that.
So by this way you can keep track of the ONLINE USERS in the CHAT APPLICATION.
Related
I am making a platform where you can only access one session per person, to do this, I had to insert in the DB a session id, then, for example:
The user logs in Chrome, at the time of login, in my DB the session id is inserted.
If the same user logs in Firefox, a new session id is inserted, and at the end it compares which was the last id so that only the last person who logged in can have access.
But the problem I have is that, if the user who logged in in Chrome does not make any movement, ie reload the page or click on another, then it stays right there and does not close the session. What I want to do is that when the user logs in on another device or browser, it automatically bounces the first session.
I know that with JS I can call that block of PHP code where I have the validation every certain time, but this would not work 100% because the platform is courses (videos) so if I make the function to run every 5 min example then you can see a course without problem the other person and if I run every 10 seconds I fear that as it sends many requests at once, you have problems with the server. Another question I have is that if I use JS and I set the function with JS then a user who has some knowledge can inspect and delete the JS and then the set code would not be executed or yes?
A more understandable example is what WhatsApp does not let in theory open another session at the same time. Any idea how to solve? Thank you very much.
I have a website where people can login, logout etc. I want to make it possible to only log in on 1 device, so if you log in with same account on lets say your phone it will give an already logged in error or so.
So I've figured out to make a "online" table which I will update to 1 on logging in and browsing page for each user, but how would I make it a 0 when someone left the page without logging out? I heard someone talking about a MySQL timer function but how does that work? So that it will set online to 0 after 300 seconds inactivity or so.
Normally you do this by updating a record in the database each time the user performs an action that indicates they're alive, or possibly via some kind of regular AJAX call that indicates they've at least got the page open.
It's generally of the form:
UPDATE users SET visited_at=NOW() WHERE id=?
Where you can provide the user's ID for that value based on the session information you have.
As far as my understanding You can try to create a column "isLogged" in the database and set it to true or false.When the user starts or end a session.
I need to know if there is an user logged in my website. For this purpose, I have only his session ID.
I got this id using: session->getId();
¿Is it possible?
Thanks in advance.
add a custom field to the session when the user logs in and then just check that field
Given that the web is essentially stateless, it is hard to know for sure if a particular user is logged onto a website.
One way to accomplish your goal is to keep a running log of all the users logged in, and the last visit time. Then you could query that log and if the users last visit time was less than 5 minutes ago, you could then say the user is logged onto your site. This will only tell you if the user is logged into your site, not if your site is the active tab in their browser.
Another way to get more "real-time" information as to the the active users of your site is to use something like SignalR which will allow you to do push to the browser. I've used SignalR in the past to send out system status messages to the browser and have it automatically update the page. The great thing about SignalR is it maintains an in-memory list of all the clients connected.
What I would like to do is essentially check whether or not a user has clicked a certain link on a page. The idea behind the functionality is that a user can click "Thanks!" on a post created on the website, but they can only submit one "Thanks!" per post.
I have no problem creating a simple click counter for the "Thanks!" button, and save the count to a database using PDO, but I am not sure how I can assign a click to the user who's session is active.Also, posts are constantly being added to the site, so I would need a system which would scale to more posts automatically.
Any guidance is appreciated, thanks!
Note: I know I have not put any example code, but I believe this is simple enough for a good programmer to grasp quickly.
EDIT
On further thought, the IP can not be used to distinguish users, as this website will be used by many people on the same IP.
Are the users logged in or anonymous? If they are logged in just save their user id in a session variable and upload it with their vote to a table in your database.
I do something similar using an Ajax request to a .php page that inserts a row with the session user ID and the vote.
If they are anonymous you can save their ip but that leads to problems with users who share the same ip address. Another (bad) option is to use cookies but users can obviously just clear their cookies and vote again.
I'm finalising my project, and I want to find how I can restore the users last page when he logs in. Do I need to have a function that save the game, or is there a way I can redirect the user to the room/page he was when he logs in. As I recall, sessions get destroyed as a user logs out.
You should store every user movement inside your website into a database with a filed like lastPageVisited and add an updater to every page of your website that update that field with the current page.
Then you could recover that field with a simple query.
But the question is: is it really worth something?