close ad
Install the LAtest Updates to Work with CC 2017 and CC 2018
open ad
View Menu

Web development tutorial

Show online users

Tutorial created by Miguel, IVCreative INC

Categories: Data Bridge, Dreamweaver, MySQL, PHP

rating

Tutorial for showing online users on your site

arrow downRequirements

To follow this tutorial you will need the webassist Security assist and follow the first steps here

http://www.webassist.com/community/tutorials/view_tutorial.php?tid=124

Step 1: Editing the pre made database

After applying the security assist plugin we will have ready our database table and at the end of the table created with the security assist you need to add on the users table 2 extra columns

`conected_usr` char(1) COLLATE latin1_general_cs DEFAULT 'N',
`tm_usr` datetime DEFAULT '0000-00-00 00:00:00',



conected_usr will then be N or Y to tell the system if a user is connected or not

tm_usr will get the last date the user connects to the system

Step 2: Edit Login page

Open the login page created by security assist, and add with dataassist a "single update record" to this page, select your users table, on "key column" select the users login name (the login name used to log in), and on Value, select the session for that users name example

php:
<?php echo $_SESSION['SecurityAssist_userEmail']; ?>



Now click on "Bindings" and search for conected_usr and on value type Y so each time a user logins to the site it will change to Y, now search for the tm_usr and add the correct php date to value field example:

php:
<?php echo date("Y-m-d H:i:s"); ?>



Save your login page.

Step 3: Update logout page

Now open the logout page generated by security assist, add with dataassist an "Update single Record", and we are going to do the same that we did on the login page setting the login table or user table, our key column the login, and on the value again the session generated by security assist.

Now on Bindings search for conected_usr bind the value to "N" and also update the tm_usr field for the exact datetime

Step 4: Creating the check who is on page

I created a new php file and i named it "whoischeck.php" give it the name you want and add this code (change it to your fields)

php:
<?php require_once('../Connections/conectpage.php'); ?>
<?php 
require_once( "../WA_SecurityAssist/Helper_PHP.php" ); ?>
<?php
////// To update session status for user table to get who is online ////////
if(WA_Auth_RulePasses("Logged in to users")){ // Begin Show Region 
$tm=date("Y-m-d H:i:s");
$xses=$_SESSION['SecurityAssist_email'];
$q=mysql_query("update usertable set conected_usr='Y', tm_usr='$tm' where UserEmail='$xses'");
echo 
$tm;
echo 
mysql_error();}
else{

}

///// ////////////// End of updating login status for who is online ///////

// Find out who is online ///////// 
$gap=10// change this to change the time in minutes, This is the time for which active users are collected. 
$tm=date ("Y-m-d H:i:s"mktime (date("H"),date("i")-$gap,date("s"),date("m"),date("d"),date("Y")));
//// Let us update the table and set the status to OFF 
////for the users who have not interacted with 
////pages in last 10 minutes ( set by $gap variable above ) ///

$ut=mysql_query("update usertable set conected_usr='N' where tm_usr < '$tm'");
echo 
mysql_error();
/// Now let us collect the userEmail from table who are online ////////
$qt=mysql_query("select UsID from usertb where tm_usr > '$tm' and conected='Y'");
echo 
mysql_error();

while(
$nt=mysql_fetch_array($qt)){
}

?>

Step 5: Include the check connected page

This last file "whoischeck.php" i include it to the sites footer with framework builder, so it loads on every page and also i set a cronjob pointing to run "whoischeck.php" in order to check every hour if the user table has activity or not.

Most hosting providers allow you to setup a cronjob specially those that come with cpanel

If you dont have cpanel to create your cronjob you can do it manually with this command

php -q /home/accountname/public_html/crons/whoischeck.php



If you still have problems setting a cronjob you would need to ask your hosting provider how can you set it up.

Step 6: Create a page to show connected users

The last and final step is to create a page with a list of users (listpage.php), you can do this with dataasist or as you would normally with dreamweaver.

If with dreamweaver add a recordset calling your user table, now add a table and set it with 2 columns and 2 rows (this part can be done with divs or what ever fits your look and feel of the website, ill explain with tables in this example).

On the first row we will add titles for the data to display, first column "Name", and on the second column we add conected .

Now on the second row bind the first column to your users names from the record set, and on the second column bind the "conected_usr", now select the full full row and add a repeat region to it.

Save and test hope this helps :)

arrow downReviews and comments

Comments will be sent to the author of this tutorial and may not be answered immediately. For general help from WebAssist, please visit technical support.

Sign in to add comments
rating
rating

Christopher West: 4 Years, 8 Months, 3 Days, 18 Hours, 26 Minutes ago

Brilliant tutorial :) I done a simular system for a website im working on however I didnt consider the cronjob part...As the dilemma I was facing is the usertable would be uploaded to a "not-loggedin" state once they click on the logout page....and some users may not click on logout as they may just close the browser...so I am guessing the cronjob would be a good way to update the usertable is the users havent been on in a while?. Thanks for posting this tutorial!

Chris

Miguel: 4 Years, 8 Months, 3 Days, 16 Hours, 12 Minutes ago

Glad to help the comunity, and yes the cronjob is to check when a user just closes the browser instead of logging out, the first part of the updates for the cronjob file, also if the user clicks any link on the page it keeps the status to on since its in the footer

khon_lao435659: 3 Years, 10 Months, 2 Weeks, 6 Days, 11 Hours, 28 Minutes ago

i have a couple of problems. first one is i get this error testing my whoischeck.php

2014-01-22 15:41:42No database selectedNo database selectedNo database selected
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/webim028/public_html/SolaDB/whoischeck.php on line 32

second problem:
there was no session for my user email. so i created $_SESSION['SecurityAssist_UserEmail'];
with security assist i added the update records for both login and logout using the new session i made, but everytime i log in it does not update my table.

code for the whoischeck.php

<?php require_once('Connections/announcement.php'); ?>
<?php require_once('webassist/security_assist/helper_php.php'); ?>
<?php


////// To update session status for user table to get who is online ////////
if(WA_Auth_RulePasses("Logged in to pcms2_users")){ // Begin Show Region
$tm=date("Y-m-d H:i:s");
$xses=$_SESSION['SecurityAssist_UserEmail'];
$q=mysql_query("update pcms2_users set connected_usr='Y', tm_usr='$tm' where UserEmail='$xses'");
echo $tm;
echo mysql_error();}
else{

}

///// ////////////// End of updating login status for who is online ///////

// Find out who is online /////////
$gap=10; // change this to change the time in minutes, This is the time for which active users are collected.
$tm=date ("Y-m-d H:i:s", mktime (date("H"),date("i")-$gap,date("s"),date("m"),date("d"),date("Y")));
//// Let us update the table and set the status to OFF
////for the users who have not interacted with
////pages in last 10 minutes ( set by $gap variable above ) ///

$ut=mysql_query("update pcms2_users set connected_usr='N' where tm_usr < '$tm'");
echo mysql_error();
/// Now let us collect the userEmail from table who are online ////////
$qt=mysql_query("select UserEmail from pcms2_users where tm_usr > '$tm' and connected_usr='Y'");
echo mysql_error();

while($nt=mysql_fetch_array($qt)){
}

?>

Miguel: 3 Years, 10 Months, 2 Weeks, 5 Days, 23 Hours, 57 Minutes ago

Hi i would need to see your file structure also you don't need to use exactly $_SESSION['SecurityAssist_UserEmail'] you can use what ever login session you have created with security assist normaly it starts with SecurityAssist_field_selected_for_login.

Same goes with "Logged in to pcms2_users" you have to use the one that you created it can be different depending on your table name, open a post on the forums and let me know here which is the link to help you out.
Miguel

khon_lao435659: 3 Years, 10 Months, 2 Weeks, 17 Hours, 35 Minutes ago

http://www.webassist.com/forums/posts.php?pid=166489

just post what i need from me there and we will go from there, thanks bud!

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.