Login | Sign up | Toll-Free: 1-800-752-0455



5 posts by 2 authors in: Forums > CMS Builder
Last Post: September 12, 2017

Hi, I'm using the membership plugin for frontend access control to an Intranet site. I want to record each login to an audit login table.

The problem I'm having is that my 'record login' code is triggered each time the page is loaded and not just on successful login and session start.

So basically I need to wrap my code in an if statement that can check if it's a new session or not?

Any Ideas?

  // If not logged in redirect
   if (!$CURRENT_USER) { websiteLogin_redirectToLogin(); }
  // If logged in set some global variables to use elsewhere
   if ($CURRENT_USER) {
     $GlobalUserNum = $CURRENT_USER['num'];
     $GlobalUserName = $CURRENT_USER['fullname'];
     $GlobalUserAccessLevel = $CURRENT_USER['access_level'];  
     // add record to login audit table 
     $tablename   = 'login_audit';
     $colsToValues = array(); 
     $colsToValues['createdDate=']     = 'NOW()';
     $colsToValues['updatedDate=']     = 'NOW()';
     $colsToValues['createdByUserNum'] = $GlobalUserNum;
     $colsToValues['updatedByUserNum'] = $GlobalUserNum;      
     $colsToValues['user_name']        = $GlobalUserName;
     $hideMissingFieldErrors = true;
     $newRecordNum = mysql_insert($tablename, $colsToValues, $hideMissingFieldErrors);   
 } // End If



Hi Ryan, 

There's actually a plugin hook in websiteMemebership.php called 'wsm_loginSuccess' that gets called on successful logins.

You can call a function with it like this:

addAction('wsm_loginSuccess', 'yourPlugin_logLogin', null, 0);

You could have a look at /cmsb/plugins/samplePlugin.php and see if you can copy that.

Alternatively, you could also check for the parameters that trigger website membership to login the user, like this: 

if (@$_REQUEST['action'] == 'login') { /* your logging code here */ }

Hope that helps!

Dave Edis - Senior Developer

Thanks Dave,

I don't really understand how to use hooks & plugins from the frontend of my application so i'll try the IF statement approach.

Should this code be added to the websiteMembership.php page or can i add to the successful login landing page?


Hi Ryan, 

Either would work.  If you add it to websiteMembership I recommend renaming the plugin websiteMembership_CUSTOM.php so it doesn't get accidentally overwritten when upgrading the plugin in future.  You could add some custom code after where wsm_loginSuccess is listed.

Or if you add it to your website files you could put it on the login landing page.  That would work as well.

Good luck with it!

Dave Edis - Senior Developer