UserModel::saveIP PHP Method

saveIP() public method

Record an IP address for a user.
public saveIP ( integer $userID, string $IP, string $dateUpdated = false ) : boolean
$userID integer Unique ID of the user.
$IP string Human-readable IP address.
$dateUpdated string Force an update timesetamp.
return boolean Was the operation successful?
    public function saveIP($userID, $IP, $dateUpdated = false)
    {
        if (!filter_var($IP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
            return false;
        }
        $packedIP = ipEncode($IP);
        $px = Gdn::database()->DatabasePrefix;
        if (!$dateUpdated) {
            $dateUpdated = Gdn_Format::toDateTime();
        }
        $query = "insert into {$px}UserIP (UserID, IPAddress, DateInserted, DateUpdated)\n            values (:UserID, :IPAddress, :DateInserted, :DateUpdated)\n            on duplicate key update DateUpdated = :DateUpdated2";
        $values = [':UserID' => $userID, ':IPAddress' => $packedIP, ':DateInserted' => Gdn_Format::toDateTime(), ':DateUpdated' => $dateUpdated, ':DateUpdated2' => $dateUpdated];
        try {
            Gdn::database()->query($query, $values);
            $result = true;
        } catch (\Exception $e) {
            $result = false;
        }
        return $result;
    }
UserModel