How to find out if a user is connected to a website using databases based on php and mysql?

I am creating a community site. Users must enter and exit as usual.

I use the attribute status online / offline to set the status of the user. But what if the user simply presses the X button or disconnects otherwise without logging out?

Recently, my computer crashed when I opened a site with my laptop, I could not log in, because I do not allow two-way access. I switch to PHPMyAdmin and I see that my status remains online. Is there any fix for this.

I tried last_time activitiy, but it does not work in case of a computer crash! And there was nothing interactivity or updating to update the table.

+6
source share
11 answers

You do not need the flag online / offline, you just need to save the last activation time. When displaying user status, if the last activity time is less than now + 15 minutes, then the user is offline, otherwise.

+12
source

Due to the nature of the Internet, you cannot know when a user disconnects, pulls a cable, or turns off his computer without politely telling you.

You can have a script check (AJAX) every X minutes to find out if the browser is responding, and if not, switch to offline mode - but this will require additional resources. This is how IRCd works: they ping you, you pong back. If you do not pong back, you go and disconnect from the server.

HTTP is stateless; there is no other built-in solution. HTML5 and sockets may be, but this will be the same principle as plain AJAX.

+3
source

You can run a function in every page request that updates a row in your database with your user ID and calculated future timestamp (for example, time()+(60*5); - five minutes). Then, when another user tries to check whether the first user is on the network, you can check it against the database using the heart rate check:

 $time = time(); $query = mysql_query("SELECT user_id, timestamp FROM online_users WHERE user_id = '$user_id' AND timestamp > '$time'"); 

If this query returns more than 0 rows, the user is considered online.

+1
source

Far decision.

Use node.js server with socket.io. Ask the client to connect to the server through the client side of socket.io. The server is responsible for sending events to clients and waiting for a response. When disconnecting or responding late, mark the user offline.

It will work and probably will work even if you disconnect the cable / close the browser, but is it worth the effort?

+1
source
 CREATE TABLE `user_online` ( `session` char(100) NOT NULL default '', `time` int(11) NOT NULL default '0' ) TYPE=MyISAM; <?php session_start(); $session=session_id(); $time=time(); $time_check=$time-600; //SET TIME 10 Minute $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="user_online"; // Table name // Connect to server and select databse mysql_connect("$host", "$username", "$password")or die("cannot connect to server"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name WHERE session='$session'"; $result=mysql_query($sql); $count=mysql_num_rows($result); if($count=="0"){ $sql1="INSERT INTO $tbl_name(session, time)VALUES('$session', '$time')"; $result1=mysql_query($sql1); } else { "$sql2=UPDATE $tbl_name SET time='$time' WHERE session = '$session'"; $result2=mysql_query($sql2); } $sql3="SELECT * FROM $tbl_name"; $result3=mysql_query($sql3); $count_user_online=mysql_num_rows($result3); echo "User online : $count_user_online "; // if over 10 minute, delete session $sql4="DELETE FROM $tbl_name WHERE time<$time_check"; $result4=mysql_query($sql4); // Open multiple browser page for result // Close connection mysql_close(); ?> 
+1
source

You are on the right track: save the time of the last UNIX user activity in the database and when someone accesses the site, set the offline status for users who have not been active for 15 minutes (or less). If the user is registered and his status is set offline in the database, force him to log out (destroying the session).

However, the real question is: is it worth it? I have not seen a similar authentication system.

0
source

I would use a combination of timeout sessions that have not been active recently and ended old sessions when a successful login attempt was made.

As Konerak said, HTTP is stateless, and there is no built-in way to tell if your site has left your site . You might be able to figure this out with some kind of JavaScript-based polling technique, but it can cause a lot of overhead that just isn't needed in most situations.

Instead, you should keep track of the last time there was any activity from the user, and save that time in your database or better yet, like memcache . If the last user’s activity time is longer than the waiting period, you decide that they are no longer on the site.

The second part will be instead of refusing registration, when someone tries to log in after logging out of the site without logging out, allowing them to log in and cancel old sessions associated with their account.

0
source

Wait for the person, I think I have the right answer, because I faced the same problem! try using the mouse event [click, movemouse ..], but with a delay, so as not to create a crash in the navigator, you will use setInterval in xx sec or minutes after saving the event response to your db in the activity column, but with unix time to differentiate him later and now. Its useful indeed, I used it to register acitvity members, to know what they really do in real time, and to get online users offline. js functions are used here:

 var body = document.getElementById('body'); var url = './member-activity.php?loggedUser=<?=$memb_id?>&curl=<?=($_SERVER['QUERY_STRING'])?>'; if(document.addEventListener) { document.addEventListener("click", _EventLogging, false); document.addEventListener("mousemove", _EventLogging, false); }else{ document.attachEvent("onclick", _EventLogging); } function _EventLogging(){ //call it after a delay of setTimeout(AjaxUrl(url), 2000); //delay of 2 sec after every move of cursor } //AjaxUrl is a js function that calls a php file to perform the activity logging via AJAX </script> <div id="id"></div> 

The div above should notify you of errors in the code, and delete it when it is ok! i hope my answer was correct for ur case // dr.alpha@hotmail.co.uk

0
source

Well, the real answer to ur question is: "How to update a table without user interaction?"

is a MySql event planner, you are planning an event (request) that should be executed at any time convenient for you, even if the user is ONLINE, read about it here ( http://dev.mysql.com/doc/refman/5.1/en/ events-overview.html )

0
source

You can use this method 1. check the status of the user if it is an online check of the last activity if it exceeds 10 minutes, the status is disabled if it is less than 10 minutes, until the user connects to the network if it is disconnected, disconnect the user offline

  1. When a user using this site updates the latest activity in each "x" min using ajax
  2. When the user clicks on the status of completion of the offline state. It could be your database structure.

enter image description here

0
source

According to your scenario, you can use the following logic to check user status:

Instead of checking whether the user is online or offline, only depending on the value of the table column, also use a session variable. for example, consider the code below:

  <?php $stauts_from_table_column; if($stauts_from_table_column==1 && isset($_SESSION['userid'])) { $user_logged_in = true; } else { $user_logged_in = false; } ?> 

You can improve it further, but in my opinion I hope.

-1
source

Source: https://habr.com/ru/post/900877/


All Articles