CREATING A LOGIN LOG TO SEE WHICH MEMBERS SIGN IN AND WHEN (UPDATED FOR MYSQLI) - Jul 22nd, 2019


Chris Waddell from Interactive Tools offered this approach:

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) Create a list field for any other fields in the user account that you'd like to display. I have a First Name and a
Last Name field, so I've created a "First Name" list field and a "Last Name" list field as above.

Each has the same list options except for the "Use This Field For Option Labels" which I changed to "first_name" and
"last_name".

If you want to add other fields to the log, just create more list fields as above, but change the "Use This Field For
Option Labels" as appropriate.

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, first_name, last_name".
Under the Viewer Urls tab, delete all the existing "Filename Fields" entries.
Under the Searching tab, set "Search Fields" to "createdDate, who, first_name, last_name".
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 following code immediately before "redirect on success":


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


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


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


In the Custom Code add (or remove) a your_field = ?, and a $CURRENT_USER['num'], for each new field you want to
populate.

Now 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'];?> - <?php echo $record['first_name:label'];?> <?php echo $record['last_:label'];?>
<hr/>
<?php endforeach ?>
</body>
</html>



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service