<![CDATA[ How-To Log Every User Login IP - My Forums]]> https://forums.modx.com/thread/?thread=44752 <![CDATA[Re: How-To Log Every User Login IP]]> https://forums.modx.com/thread/44752/how-to-log-every-user-login-ip#dis-post-257460 presson83 Mar 27, 2009, 09:01 AM https://forums.modx.com/thread/44752/how-to-log-every-user-login-ip#dis-post-257460 <![CDATA[Re: How-To Log Every User Login IP]]> https://forums.modx.com/thread/44752/how-to-log-every-user-login-ip#dis-post-257459 ]]> rethrash Mar 27, 2009, 08:51 AM https://forums.modx.com/thread/44752/how-to-log-every-user-login-ip#dis-post-257459 <![CDATA[Re: How-To Log Every User Login IP]]> https://forums.modx.com/thread/44752/how-to-log-every-user-login-ip#dis-post-257458 presson83 Mar 26, 2009, 02:07 PM https://forums.modx.com/thread/44752/how-to-log-every-user-login-ip#dis-post-257458 <![CDATA[Re: How-To Log Every User Login IP]]> https://forums.modx.com/thread/44752/how-to-log-every-user-login-ip#dis-post-257457

//Check this out http://modxcms.com/forums/index.php/topic,34174.msg207863.html#msg207863]]>
mrhaw Mar 26, 2009, 02:03 PM https://forums.modx.com/thread/44752/how-to-log-every-user-login-ip#dis-post-257457
<![CDATA[How-To Log Every User Login IP]]> https://forums.modx.com/thread/44752/how-to-log-every-user-login-ip#dis-post-257456
Just thought I’d share this with you all. A client recently requested that they be able to see every unique IP address of users that login to the site. This snippet - http://modxcms.com/forums/index.php/topic,4759.0.html explains how to add ip logging when people register by adding code to websignup.inc.php. I however needed to log every time they logged in, so I did a bit of re-working and here’s how you can accomplish the same thing:

1. First off, backup your DB and weblogin.inc.php before you begin.

2. Create a new table in your database titled "ip_log" with the exact same structure as the active_users table. This table will be used to log every single login on it’s own row.

3. Open up assets/snippets/weblogin.inc.php and find
        $sql = "REPLACE INTO $dbase.`".$table_prefix."active_users` (internalKey, username, lasthit, action, id, ip) values(-".$_SESSION['webInternalKey'].", '".$_SESSION['webShortname']."', '".$lasthittime."', '".$a."', ".$itemid.", '".$ip."')";


after that line, add

 $sql2 = "INSERT INTO $dbase.`".$table_prefix."ip_log` (internalKey, username, lasthit, action, id, ip) values(".$_SESSION['webInternalKey'].", '".$_SESSION['webShortname']."', '".$lasthittime."', '".$a."', ".$itemid.", '".$ip."')";
		
		$sql3 = "SELECT * FROM $dbase.`".$table_prefix."web_user_attributes` WHERE `internalKey` = '".$_SESSION['webInternalKey']."'";
		
		$result = $modx->dbQuery($sql3);
		
		$comment = mysql_result($result, 0, 'comment');
		
		$uniqueComment = strstr($comment, $ip);
		
		
		if ($uniqueComment === FALSE) {
			$sql4 = "UPDATE $dbase.`".$table_prefix."web_user_attributes` SET `comment` = CONCAT(`comment`, '
$ip') WHERE `internalKey` = '".$_SESSION['webInternalKey']."'";

			if(!$rs = $modx->dbQuery($sql4)) {
				$output = "error replacing into active users! SQL: ".$sql4;
				return;
			}
		}


3. Find this code

        if(!$rs = $modx->dbQuery($sql)) {
            $output = "error replacing into active users! SQL: ".$sql;
            return;
        }


after that line insert

		if(!$rs = $modx->dbQuery($sql2)) {
            $output = "error replacing into active users! SQL: ".$sql2;
            return;
        }



4. Lastly, the page that the user is re-directed to post-login will need to have the call to the [!WebLogin?!] snippet. This is pretty much always the case because you want the "logout" link to appear somewhere, but just make sure of that.

That’s pretty much it! From this point forward, everytime a user logs into your site their IP address will be recorded in both the ip_log table and the web_user_attributes tables. In the web_user_attributes table the ip will only be logged if it’s unique. It is placed into the comment field so that it will be visible in modx via the back-end admin. The comment box is not used by default so it is a good place for the ip’s unless you are using it.

I’m sure there are more elegant ways of doing this, but this was the best I could do. Any ideas on how to improve on this would be awesome. Hope it’s helpful!]]>
presson83 Mar 26, 2009, 01:35 PM https://forums.modx.com/thread/44752/how-to-log-every-user-login-ip#dis-post-257456