Retrieve and display logged in users

Author: Günter Schenk, Added: 20.01.2008, Updated: 21.01.2008


Table Of Contents

Title Version
The Scope n/a
Adding the column to your user table n/a
Adding a Custom Trigger to your Log In page n/a
Adding a Custom Trigger to your Log Out page n/a
Displaying a list of all currently logged in users n/a


print this page... 1. The Scope


Honestly said :: if I were to create a forum application with the Dreamweaver Developer Toolbox, I´d certainly want to implement some advanced functionality for e.g. detecting and displaying the number of all currently logged in users -- but can we do such cool stuff with ADDT ? Yes, we can :-)

This tutorial is based on three additions to ADDTs default "User Login" handling :: simply said, we´re going to add...

As you´ll see, this is actually the only part that´s maybe somewhat "tricky" and will require a little understanding of how to adapt the provided code examples to your own ADDT login setup -- on the other hand it´s not that complicated, and of course I´ll be walking you through the steps to get there.

I´m sure that the benefits of having an extra "logged_in" column in your table is self-evident :: only by tracking this very status you´ll be able to implement advanced applications like e.g. an Online Chat module for your intranet site.

jump to page top...


print this page... 2. Adding the column to your user table


Assuming your ADDT login table table is named "login", the MySQL compatible SQL for adding this column is quite simplistic:

ALTER TABLE `login` ADD `logged_in` CHAR( 1 ) NULL DEFAULT 'N';

Some notes on the parameters being used in this example:

After adding this column to your Dreamweaver Developer Toolbox "login" table, let´s move on to the next step...

jump to page top...


print this page... 3. Adding a Custom Trigger to your Log In page


3.1. Explaining the Custom Trigger´s Code

The following "Custom Trigger" needs to be added to your ADDT "Log In" page and will be updating the "logged_in" column by setting its value to 'Y':

$update_logged_in_status = "UPDATE login SET logged_in = 'Y' WHERE id = ".$_SESSION["kt_login_id"];
$update_result = $tNG->connection->execute($update_logged_in_status);
if(!$update_result) {
$updateError = new tNG_error("Error setting the logged in status to Y", array(), array());
return $updateError;
} else {
return NULL;
}


As it´s obvious that this update will have to exclusively affect the record that´s related to the current user´s "Primary Key" column, the WHERE clause...

WHERE id = ".$_SESSION["kt_login_id"]

...will make sure that the Primary Key "id" will match ADDT´s session variable "kt_login_id".

As you probably will need to adapt this query to match the *names* of your own ADDT "login" table respectively its Primary Key column, please help yourself with the following explanation on what components to modify:

ADDT Custom Trigger: the Update query explained


3.2. Adding the Custom Trigger with Dreamweaver & ADDT

a) select "Developer Toolbox -> Forms -> Custom Trigger" from the "Server Behaviours" panel and paste the Custom Trigger´s Code into the "Basic" tab´s field:

ADDT Custom Trigger: the Basic tab

b) switch to the "Advanced" tab and rename the default Trigger name to "Update_Login_Status":

ADDT Custom Trigger: the Advanced tab

As a default Dreamweaver Developer Toolbox "Log In" page will just contain a Login Transaction, the "Register trigger to transaction(s)" settings displayed above will not have to be changed.

jump to page top...


print this page... 4. Adding a Custom Trigger to your Log Out page


4.1. Explaining the Custom Trigger´s Code

The following "Custom Trigger" needs to be added to your ADDT "Log Out" page and will be updating the "logged_in" column by setting its value to 'N':

if ( isset($_GET['user_id']) && !empty($_GET['user_id']) ) {
$update_logged_in_status = "UPDATE login SET logged_in = 'N' WHERE id =".$_GET["user_id"];
$update_result = $tNG->connection->execute($update_logged_in_status);
if(!$update_result) {
$updateError = new tNG_error("Error setting the logged in status to N", array(), array());
return $updateError;
} else {
return NULL;
}
}

This trigger´s code is slightly more complicated than the one we have been using on the "Log In" page, and you might note that the current WHERE condition will *not* be checking against the default Dreamweaver Developer Toolbox "kt_login_id" session variable -- why ? Well, it would have been way cool if it *were* possible to use any of ADDT´s session variables on the "Log Out" page, but the problem is :: once this page loads, all PHP session variables are getting immediately the next link leads to an external website destroyed, means that we can´t fetch and evaluate them for our custom purpose -- they don´t exist any longer, and any query that´s trying to use them will fail miserably.

As I was desparately in search for a workaround just recently, I found out that I´ll just need to pass ADDT´s "kt_login_id" session variable as *URL parameter* to my Log Out page. This indeed works just fine, and all one has to do is to "harden" this Custom Trigger by enclosing the whole thing in a PHP "if" control structure which checks if two conditions will both return true in order to execute this Custom Trigger:
This is how the complete condition is built:

if ( isset($_GET['user_id']) && !empty($_GET['user_id']) ) {
......
}


4.2. Adding the "user_id" URL variable to the "Log Out" link

Regardless if you´re creating a new link to your ADDT "Log Out" page, or if you´re going to edit an existing link, the procedure is the same:

a) highlight the link that´s pointing to your ADDT "logout" page

b) click the "Browse for File" icon in Dreamweaver´s "Properties" panel

c) click the "Parameters" button

d) enter "user_id" in the "Name" field:

Dreamweaver Link Parameters: enter the Name user_id

e) click the button next to the "Value" field and select the session variable "kt_login_id":

Dreamweaver Link Parameters: select the Session Variable kt_login_id


Important Note :: If your link to ADDT´s "Log Out" page has been established on a manually created PHP page, please make sure to add...

session_start();

...to line 1, otherwise this page will most probably not be able to detect the running PHP session respectively the current user´s "kt_login_id" value -- if it doesn´t, the URL parameter "user_id" will be NULL, and the Log Out page will not be executing the Custom Trigger.

4.3. Adding the Custom Trigger with Dreamweaver and ADDT

a) select "Developer Toolbox -> Forms -> Custom Trigger" from the "Server Behaviours" panel and paste the Custom Trigger´s Code into the "Basic" tab´s field:

ADDT Custom Trigger :: The Basic tab

b) switch to the "Advanced" tab and rename the default Trigger name to "Update_Login_Status":

ADDT Custom Trigger :: The Advanced tab

In opposition to the Custom Trigger that has been added to your "Log In" page, this very Trigger will have to be executed BEFORE the Logout Transaction happens

jump to page top...


print this page... 5. Displaying a list of all currently logged in users


5.1. Prepare your page for displaying the list

a) as usual, we´ll need to establish a standard Dreamweaver recordset like the following one:

Dreamweaver Recordset: Show who is logged in

In this example I´m just displaying all logged in users "real names", and that´s why I suggest to simply...
Dreamweaver should now display the result like this:

Who is logged in right now?

{who_is_logged_in.realname}


b) highlight the Dynamic Data instance "realname" including the following line break and apply a Dreamweaver "Repeat Region" server behaviour in order to show all records


Dreamweaver Repeat Region: select all records

c) as the recordset might also return no entries at all, hight the whole DIV container and apply a Dreamweaver "Show If Recordset Is Not Empty" server behaviour:

Dreamweaver: Show If Recordset Is Not Empty


5.2. What now ?

Well, that´s basically all there is to do, and the next time you´ll log in to your account and view this page in your browser, you will see -- your own name listed as well !! Is this something you really want ??

In case you´d rather prefer to see all names except your own one, you´ll need to modify the "who_is_logged_in" recordset manually :: switch the page to CODE, try to locate the $query_who_is_logged_in query and add the follwing AND condition to it

Code: adding an AND condition to the WHERE clause

What the additional...

AND id != '".$_SESSION['kt_login_id']."'

... condition actually does, is to exclude (!=) the record whose Primary Key (in this case the "id") matches a user´s "_kt_login_id" session variable -- and as you might guess, this will always be the person who´s logging in into his account.

Please note:
when using this "don´t show my name to myself" option, please add session_start(); to line 1, otherwise the session doesn´t exist on a manually created PHP document


jump to page top...


Copyright Note:
This tutorial and all related material (files, images etc) are licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
Contact:
info @ guenter-schenk.com