We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    Is there a MODx method for getting request data? I want to log IPs and User Agent strings... I can read this out of the various server variables, but I was wondering if MODx already had built out some of this somewhere...

    Thanks!
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      No need for any wrappers for that; a snippet or plugin is the way to get the SERVER variables.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 3749
        • 24,544 Posts
        I was just playing around with a plugin to log page-not-found requests. I'm writing them to a chunk, but you could just as well write them to a file. I don't know if the double sanitizing is really necessary or not. The data is stored as a comma-separated list, so you'd need a snippet to display it properly (the function that writes the data adds a "\n" at the end).

        <?php
        function getHost($ip){
                $ptr= implode(".",array_reverse(explode(".",$ip))).".in-addr.arpa";
                $host = dns_get_record($ptr,DNS_PTR);
                if ($host == null) return $ip;
                else return $host[0]['target'];
            }
        
        $data['page'] = htmlentities(strip_tags($_SERVER['REQUEST_URI']));
             $data['ip'] = htmlentities(strip_tags($_SERVER['REMOTE_ADDR']));
             $data['userAgent'] = htmlentities(strip_tags($_SERVER['HTTP_USER_AGENT']));
             $data['host'] = getHost($ip);
             $msg = implode(',', $data);
        
             /* log data */
             my_debug($msg);
        
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting