Mecha CMS

Code snippet collection for developers.

Grab User’s Comment IP and UA

Store user’s IP and user’s UA string on every comment creation event.

Requires comment extension.

Hook::set('on.comment.set', function() {
    $folder = Path::F($this->path);
    if ($ip = Get::IP()) {
        File::put($ip)->saveTo($folder . DS . 'ip.data', 0600);
    }
    if ($ua = Get::UA()) {
        File::put($ua)->saveTo($folder . DS . 'ua.data', 0600);
    }
});

Then, you can call the stored comment IP and UA this way:

foreach ($page->comments as $comment) {
    echo '<p><strong>IP:</strong> ' . $comment->ip . '</p>';
    echo '<p><strong>UA:</strong> ' . $comment->ua . '</p>';
}