moodle/moodle · error · moodle_exception

invalidipformat

invalidipformat

Error message

invalidipformat

What it means

public/iplookup/index.php throws moodle_exception 'invalidipformat' when the user-supplied IP parameter fails cleanremoteaddr() validation. The IP string is not a syntactically valid IPv4/IPv6 address.

Solutions

  1. Pass a valid, properly formatted IP address in the ip parameter.
  2. Normalize the value with cleanremoteaddr() before redirecting to the iplookup page.
  3. If the input comes from logs, sanitize/validate it and skip lookup when it is not a real IP.
  4. Decode/URL-encode the parameter correctly — encoded or truncated IPv6 fails validation.

Example fix

// before
redirect('/iplookup/index.php?ip=' . $host); // hostname -> invalidipformat
// after
$ip = cleanremoteaddr($host);
if ($ip !== false) {
    redirect('/iplookup/index.php?ip=' . $ip);
}
Defensive patterns

Strategy: validation

Validate before calling

function isValidIp(ip) {
  return typeof ip === 'string' &&
    /^(\d{1,3}(\.\d{1,3}){3}|[0-9a-fA-F:]+)$/.test(ip) &&
    ip.indexOf(':') !== -1 ? ip.includes('::') || ip.split(':').length > 2 : ip.split('.').every(n => +n <= 255);
}
// PHP side: cleanremoteaddr($ip) !== false before linking to iplookup

Type guard

function isIpString(v) {
  return typeof v === 'string' && v.length > 0 && (net.isIP(v) !== 0);
}

Try / catch

try {
  render_iplookup($ip);
} catch (moodle_exception $e) {
  if ($e->errorcode === 'invalidipformat') {
    echo 'Provided value is not a valid IP address.';
  }
}

Prevention

When it happens

Trigger: Visiting /iplookup/index.php?ip=<value> where cleanremoteaddr($ip) returns false — e.g. garbage input, 'unknown', hostname instead of IP, or a malformed IPv6 string.

Common situations: Manually crafting the iplookup URL with a hostname or obfuscated value, logging systems storing placeholder values like '0.0.0.0 hidden', plugins passing unparsed log tokens into the lookup.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/c843652216a27d85. Report an issue: GitHub.

Appendix: source

Thrown at public/iplookup/index.php:74

    $ispopup = 1;
} else if ($ispopup === 1) {  // Param popup was set, then we know that we want a popup.
    $urlparams['ispopup'] = 1;
}
// Set the page layout accordingly.
if ($ispopup) {
    $PAGE->set_pagelayout('popup');
} else {
    $PAGE->set_pagelayout('standard');
}

$PAGE->set_url('/iplookup/index.php', $urlparams);
$PAGE->set_context(context_system::instance());

$info = array($ip);
$note = array();

if (cleanremoteaddr($ip) === false) {
    throw new \moodle_exception('invalidipformat', 'error');
}

if (!ip_is_public($ip)) {
    throw new \moodle_exception('iplookupprivate', 'error');
}

$info = iplookup_find_location($ip);

if ($info['error']) {
    // Can not display.
    notice($info['error']);
}

if ($user) {
    if ($user = $DB->get_record('user', array('id'=>$user, 'deleted'=>0))) {
        // note: better not show full names to everybody
        if (has_capability('moodle/user:viewdetails', context_user::instance($user->id))) {
            array_unshift($info['title'], fullname($user));

View on GitHub (pinned to 48ea8c3322)