Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying a online user list using PHP
    text
    copied!<p>Hello everyone so I have partly working code. However I also have issues with it. But let me start off to tell you what I am trying to achieve... On my site I have a login facility which sends all users to a home page once and only if the user is logged in. I would like to display who else is logged in on my page showing there usernames in a list. </p> <h3>My codes current ability...</h3> <p>Okay so i can code it to add the users on joining the site to a database and also then display each user on that database into a div which is great as it displays everyone online... </p> <h3>Where the problem stands...</h3> <p>However, when a user leaves the page there username needs to be deleted from the online user database. In the past I would simply do this with a on window close option in java. But due to Google safari and Firefox no longer supporting this option I am forced to find another way of doing it.</p> <h3>What the code bellow does...</h3> <p>So the code bellow loads an interval so after so much time it will repeat the code within it. The code first adds the user to the database, it then displays the user in a list on the site and then it deletes the user from the database just in case they are to then go offline.</p> <h3>Where the problem stands...</h3> <p>This all being very well for one user, but once a second user gets involved there intervals fall at a different time. So I have deleted the user from the database after it shows the list but then it means the second user displays the database and the first user isn't in it as they are deleted... This also causes a problem should a user go offline half way though the function happening, then it leaves them in the database and doesn't get around to deleting them out of the database.</p> <h3>The following is the java code...</h3> <pre><code> $(document).ready(function() { var user_name = "&lt;?php print $username ?&gt;"; setInterval(function() { $.post('onlineusers.php', { name: user_name, action:"joined" }); $.post("onlineusers.php", { action2: "list" }, function(data){ $('#listusers').html(data); }); $.post("onlineusers.php", { nameleft: user_name, action3:"left" }, function(data){ $('#errorrreport').html(data); }); }, 5000); }): </code></pre> <h3>Now there is the PHP document code</h3> <pre><code> //------------User joined page put into database -------- if( $_REQUEST["name"]) { $user_name = $_REQUEST['name']; }; if( $_REQUEST["action"]) { $action = $_REQUEST['action']; }; if ($action == 'joined') { user_joined($user_name); }; //-----------Listing online users within page ------------- if( $_REQUEST["action2"]) { $action2 = $_REQUEST['action2']; }; if($action2 == 'list') { foreach (user_list() as $user){ echo $user . "&lt;br /&gt;"; }; }; //------------ User left delete from the database ------------ if( $_REQUEST["nameleft"]) { $user_nameleft = $_REQUEST['nameleft']; }; if( $_REQUEST["action3"]){ $action3 = $_REQUEST['action3']; }; if($action3 == 'left') { user_left($user_nameleft); }; //------ Functions what to do... -------- function user_joined($user_name) { $user_name = mysql_real_escape_string(htmlentities($user_name)); mysql_query("INSERT INTO users (user_name)VALUES('$user_name')") or die("this didn't happen"); } function user_left($user_nameleft) { $user_name = mysql_real_escape_string(($user_nameleft)); $query = mysql_query("DELETE FROM users WHERE user_name = '$user_nameleft'")or die("failed to delete from table"); } function user_list() { $user_list = array(); $users_query = mysql_query("SELECT user_name FROM users") or die ("Unable to collect userlist"); while ($users_row = mysql_fetch_assoc($users_query)){ $user_list[] = $users_row['user_name']; } return $user_list; } </code></pre> <h3>The above code</h3> <p>Sorry it is slightly messy due to my recoding of it so many times in order to get it to work. I would appreciate it if anyone could give me any help towards getting this to work. Now if it's not just simple off the code from above I can add the user to the online user database on entrance to the page and get it to list who is online frequently from listing the users on the database. </p> <h2>The real issue and where i am asking for your help with please...</h2> <p>However, if you have any idea on the code for the following that would be great... The user leaving the page is where the problem comes up. I need some method of checking the user is still active and if they are not then deleting them from the database so when the list then refreshes the user is no longer on the list. Such as pinging something until it ends up with no response and deletes the user from the database. </p> <p>As my current code has problems with multiple users and synchronization of the interval function it needs to take that into account different users will see the list refresh at different times.</p> <p>P.S. I have also looked at using $SESSION however i am still not sure on how to make this work with checking for offline users and then deleting them from the database this might be a method of doing it.</p> <p>Thank you, I hope there is enough information to go on. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload