We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23849
    • 223 Posts
    Hi All,

    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!
      Nick Hoag
      Creative Partner
      The FutureForward

      http://thefutureforward.com
      • 20413
      • 2,877 Posts
      Awesome you did it and you reported back!! TNX!!  cool

      //Check this out http://modxcms.com/forums/index.php/topic,34174.msg207863.html#msg207863
        @hawproductions | http://mrhaw.com/

        Infograph: MODX Advanced Install in 7 steps:
        http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

        Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
        http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
        • 23849
        • 223 Posts
        np mrHaw!
          Nick Hoag
          Creative Partner
          The FutureForward

          http://thefutureforward.com
        • You should include this in the Extras section of the main site. Great job. smiley
            Ryan Thrash, MODX Co-Founder
            Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
            • 23849
            • 223 Posts
            Thanks Ryan, will do!
              Nick Hoag
              Creative Partner
              The FutureForward

              http://thefutureforward.com