Notifications on user login
There are times when you might want to know when a certain user logs into the system. The following code will send email notifications when a Web User or a Manager User access the system.
The plugin will make use of the OnWebLogin and OnManagerLogin events.
The plugin
Create a new plugin and paste the following code:
$e = &$modx->Event;
global $modx;
if($email && $wun && $e->name == "OnWebLogin"){//check Web User Names
$aWun = explode(',',$wun);
foreach($aWun as $un){//loop usernames
if($username == trim($un)){
$subject = 'Web User Login';
$message = $username . ' has logged in on ' . date('l jS \of F Y h:i:s A');
sendEmail($email, $subject, $message);
}
}
}
if($email && $mun && $e->name == "OnManagerLogin"){//check Manager User Names
$aMun = explode(',',$mun);
foreach($aMun as $un){//loop username
if($username == trim($un)){
$subject = 'Manager User Login';
$message = $username . ' has logged in on ' . date('l jS \of F Y h:i:s A');
sendEmail($email, $subject, $message);
}
}
}
// Send email
function sendEmail($to, $subject, $message) {
$headers ="MIME-Version: 1.0\n".
"Content-type: text/html; charset=iso-8859-1\n".
"From: ".$_SERVER['SERVER_NAME']."<no-reply@{$_SERVER['HTTP_HOST']}>\n".
"X-Mailer: PHP/".phpversion();
$body = <<<EOD
$message
EOD;
$mail = mail($to, $subject, $body, $headers);
}
Name the plugin accordingly. Eg:Notifications on Login
Configuration
Go to the Configuration Tab and paste the following in the Plugin configuration box:
&wun=Web User Names;text;&mun=Manager User Names;text;&email=Email to:;text;
Click Update Parameter Display and enter your configuration for the plugin. You can track multiple users by entering a comma separated list of usernames.
One more step
Go to System Events and check OnWebLogin and OnManagerLogin events. Save the plugin.
Any other events you might want to track? Leave a message.
Write a comment