Website membership - Pages Visited / Logoff Time

By dm - January 17, 2013

Hi,

I have a login log page set up to record details of when users enter the membership area pages giving me a simple list with username, time entered and ip as per the following posts

http://www.interactivetools.com/forum/forum-posts.php?postNum=2217409#post2217409

http://www.interactivetools.com/forum/forum-posts.php?postNum=2213350#post2213350

Does anyone know a way to record when the user logs off (or logged on time) and what membership pages were visited?

Many thanks for you help!

Hi,

There is a way you can do this, but it does require you to modify the code of the website membership plugin slightly, which isn't something we recommend. So if you upgrade the plugin at a later date, you will have to add this feature back into it. Also this will only record users when they press the log off button, if they let there session expire or close the browser then there isn't really a way you can record the action.

First you'll need to create a section that can store the information. I've attached a screenshot of how I set up my section, I named the section logged off.

Next you'll need to open your websiteMembership.php file from your plugins folder in CMS Builder.

You'll need to find the _websiteLogin_logoff function which will probably be around line 150. Then add the following code that is highlighted in green at the beginning of the function:

// remove login cookies

function _websiteLogin_logoff() {

global $CURRENT_USER;

mysql_insert('logged_off', array('userNum' => $CURRENT_USER['num'], 'username' => $CURRENT_USER['username'], 'time' => date('Y-m-d H:i:s'), 'ip' => $_SERVER['REMOTE_ADDR']));

// get logoff url
if ($GLOBALS['WEBSITE_LOGIN_POST_LOGOFF_URL']) {

......

The global $current user will get the current users data, and the mysql_insert function will add a new record to the logged_off section.

Let me know if you have any problems implementing this system.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com
Attachments:

section-layout_002.png 6K

Hi DM,

A while back I created a login log that showed when a member last logged in. You might be able to use some of the ideas and combine them to include the logoff as well.

Here’s the recipe from my CMSB Cookbook http://www.thecmsbcookbook.com that describes the process.

CREATING A LOGIN LOG TO SEE WHICH MEMBERS SIGN IN AND WHEN

NOTE: Records will only be added to the "Login Log" section for actual membership logins, not by logins to the CMSB interface.

1) Create a Multi Record section called "Login Log".

2) Modify it and remove all the fields except "num" and "createdDate" (note that you'll need to "Enable System Field Editing" under the "Advanced Commands..." dropdown to remove some of the fields.)

3) Add a List Field called "Who". Leave "Display As" set to "pull down".

Modify your "Who" field as follows:

List Options: Get options from database (advanced)

Section Tablename: accounts
Use this field for option values: num
Use this field for option labels: username


4) Now, make some more changes to your Login Log section, this time at the top of the page:

In the General tab, change "ListPage Fields" to "createdDate, who".
Under the Viewer Urls tab, delete all the existing "Filename Fields" entries.
Under the Searching tab, set "Search Fields" to "who, createdDate".
Finally, under the Sorting tab, set "Order By" to "createdDate DESC". Now click Save Details.

5) Now open up cmsAdmin/plugins/websiteMembership/websiteMembership.php in a text editor and find "redirect on success". Add the code below before "redirect on success":

// CUSTOM CODE! add record to login_log
global $TABLE_PREFIX;
mysql_query(mysql_escapef("INSERT INTO {$TABLE_PREFIX}login_log SET createdDate = NOW(), who = ?", $CURRENT_USER['num']))
or die("Mysql error adding login_log record: ". htmlspecialchars(mysql_error()) . "\n");

If you don't want to log admins, insert this code instead:

// CUSTOM CODE! add record to login_log
if (!@$CURRENT_USER['isAdmin'])
{global $TABLE_PREFIX;
mysql_query(mysql_escapef("INSERT INTO {$TABLE_PREFIX}login_log SET createdDate = NOW(), who = ?", $CURRENT_USER['num']))
or die("Mysql error adding login_log record: ". htmlspecialchars(mysql_error()) . "\n");
}

After you've created this log, you can create a list viewer that your client can access and restrict viewer access to admins only.

Here's a simple example that you can style to match your site design.

At the top of your listing page:
NOTE: Don't forget to change the $dirsToCheck = array('/PATH_TO_YOUR_SERVER/','','../','../../','../../../'); to match your server path. (you can find this in the admin>code generator code for any section.

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/PATH_TO_YOUR_SERVER/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records
list($login_logRecords, $login_logMetaData) = getRecords(array(
'tableName' => 'login_log',
));
if (!$CURRENT_USER['isAdmin']) { websiteLogin_redirectToLogin(); }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login Log</title>
</head>
<body>

<?php foreach ($login_logRecords as $record): ?>
<?php echo date("F jS Y, g:i a ", strtotime($record['createdDate'])) ?>

<?php echo $record['who:label'];?>
<hr/>
<?php endforeach ?>
</body>
</html>

Hope that gives you some more ideas...

If you end up creating something, please post it so I can update the recipe in the Cookbook.

Best,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php