<?php
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/functions.php';

$page_title   = 'IP Lookup – Location, ISP and Network Info';
$page_desc    = 'Free IP Lookup: find location, ISP, ASN, hostname and network information for any IPv4 or IPv6 address on ipcheck.tools.';
$page_current = 'ip-lookup';

$query = trim(strip_tags($_GET['ip'] ?? ''));
$geo   = [];
$error = '';
$own   = false;

if ($query) {
    if (!filter_var($query, FILTER_VALIDATE_IP)) {
        $resolved = @gethostbyname($query);
        if ($resolved !== $query) {
            $query = $resolved;
        } else {
            $error = "Invalid IP address or hostname: <strong>" . h($query) . "</strong>";
        }
    }
    if (!$error) {
        $geo = get_geoip($query);
    }
} else {
    $ips  = get_visitor_ips();
    $query = $ips['ipv4'] ?: $ips['ipv6'];
    $geo  = get_geoip($query);
    $own  = true;
}

$hostname = $query ? get_hostname($query) : '';

require_once __DIR__ . '/header.php';
?>

<div class="hero">
  <div class="hero-label">Tool</div>
  <div class="hero-title">IP Lookup</div>
  <div class="hero-sub">Location, provider and network information for any IP address</div>
  <form method="GET" action="/en/ip-lookup.php">
    <div class="input-group">
      <input class="input-text" type="text" name="ip"
             value="<?= h($own ? '' : $query) ?>"
             placeholder="e.g. 8.8.8.8 or 2001:4860:4860::8888"
             />
      <button type="submit" class="btn btn-primary">
        <i class="ti ti-world-search"></i> Query
      </button>
    </div>
  </form>
</div>

<div class="wrap">

  <div class="ad-slot"><ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-8287576653347400" data-ad-slot="2715725452" data-ad-format="auto" data-full-width-responsive="true"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script></div>

  <?php if ($error): ?>
    <div class="alert alert-danger"><i class="ti ti-alert-circle"></i><span><?= $error ?></span></div>

  <?php elseif ($query && $geo): ?>

    <?php if ($own): ?>
    <div class="alert alert-info">
      <i class="ti ti-info-circle"></i>
      <span>Your own IP address is being shown. Enter a different IP above to look it up.</span>
    </div>
    <?php endif; ?>

    <div class="stat-grid">
      <div class="stat">
        <div class="stat-val" style="color:<?= ($geo['is_private'] ?? false) ? 'var(--orange)' : 'var(--green)' ?>; font-size:13px;">
          <?= ($geo['is_private'] ?? false) ? 'Private' : 'Public' ?>
        </div>
        <div class="stat-key">IP Type</div>
      </div>
      <div class="stat">
        <div class="stat-val" style="font-size:13px; color:<?= filter_var($query, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ? 'var(--purple-dim)' : 'var(--blue-dim)' ?>;">
          <?= filter_var($query, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ? 'IPv6' : 'IPv4' ?>
        </div>
        <div class="stat-key">Version</div>
      </div>
      <div class="stat">
        <div class="stat-val" style="font-size:12px; color:var(--blue-dim);">
          <?php if ($geo['asn']): ?>
            <a href="/en/asn.php?asn=<?= urlencode($geo['asn']) ?>" style="color:var(--blue-dim);"><?= h($geo['asn']) ?></a>
          <?php else: ?>–<?php endif; ?>
        </div>
        <div class="stat-key">ASN</div>
      </div>
      <div class="stat">
        <div class="stat-val" style="font-size:13px;">
          <?= $geo['country_iso'] ? country_flag($geo['country_iso']) : '–' ?>
        </div>
        <div class="stat-key">Country</div>
      </div>
    </div>

    <div class="grid2">
      <div class="card card-blue">
        <div class="card-title"><i class="ti ti-map-pin"></i> Location</div>
        <div class="data-row"><span class="dk">IP Address</span><span class="dv mono blue"><?= h($query) ?></span></div>
        <div class="data-row"><span class="dk">City</span><span class="dv"><?= h($geo['city'] ?: '–') ?></span></div>
        <div class="data-row"><span class="dk">State / Region</span><span class="dv"><?= h($geo['region'] ?: '–') ?></span></div>
        <div class="data-row"><span class="dk">Country</span><span class="dv">
          <?= $geo['country_iso'] ? country_flag($geo['country_iso']) : '' ?>
          <?= h($geo['country'] ?: '–') ?>
        </span></div>
        <div class="data-row"><span class="dk">Postal code</span><span class="dv mono"><?= h($geo['postal'] ?: '–') ?></span></div>
        <div class="data-row"><span class="dk">Timezone</span><span class="dv mono"><?= h($geo['timezone'] ?: '–') ?></span></div>
        <?php if ($geo['lat'] && $geo['lon']): ?>
        <div class="data-row"><span class="dk">Coordinates</span><span class="dv mono"><?= round($geo['lat'], 4) ?>° N · <?= round($geo['lon'], 4) ?>° E</span></div>
        <?php endif; ?>
      </div>

      <div class="card">
        <div class="card-title"><i class="ti ti-building"></i> Network & Provider</div>
        <div class="data-row"><span class="dk">ISP / Provider</span><span class="dv"><?= h($geo['isp'] ?: $geo['asn_org'] ?: '–') ?></span></div>
        <div class="data-row"><span class="dk">Organisation</span><span class="dv"><?= h($geo['asn_org'] ?: '–') ?></span></div>
        <div class="data-row"><span class="dk">ASN</span><span class="dv mono">
          <?php if ($geo['asn']): ?>
            <a href="/en/asn.php?asn=<?= urlencode($geo['asn']) ?>" style="color:var(--blue-dim);"><?= h($geo['asn']) ?></a>
          <?php else: ?>–<?php endif; ?>
        </span></div>
        <div class="data-row"><span class="dk">Hostname / PTR</span><span class="dv mono" style="font-size:11px;"><?= h($hostname ?: '–') ?></span></div>
        <div class="data-row"><span class="dk">IP Version</span><span class="dv">
          <?= filter_var($query, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)
            ? '<span class="badge badge-v6">IPv6 (128-bit)</span>'
            : '<span class="badge badge-v4">IPv4 (32-bit)</span>' ?>
        </span></div>
        <div class="data-row"><span class="dk">Type</span><span class="dv">
          <span class="badge <?= ($geo['is_private'] ?? false) ? 'badge-orange' : 'badge-green' ?>">
            <?= ($geo['is_private'] ?? false) ? 'Private' : 'Public' ?>
          </span>
        </span></div>
      </div>
    </div>

    <?php if ($geo['lat'] && $geo['lon']): ?>
    <div class="card">
      <div class="card-title"><i class="ti ti-map"></i> Approximate Location</div>
      <div style="text-align:center; padding:20px 0;">
        <i class="ti ti-map-pin" style="font-size:32px; color:var(--text4); display:block; margin-bottom:8px;"></i>
        <div style="font-family:var(--font-mono); color:var(--text3);"><?= round($geo['lat'], 4) ?>° N · <?= round($geo['lon'], 4) ?>° E</div>
        <a href="https://www.openstreetmap.org/?mlat=<?= $geo['lat'] ?>&mlon=<?= $geo['lon'] ?>&zoom=10" target="_blank" rel="noopener"
           class="copy-btn" style="margin-top:12px; display:inline-flex;">
          <i class="ti ti-external-link"></i> Open in OpenStreetMap
        </a>
      </div>
    </div>
    <?php endif; ?>

    <div class="card">
      <div class="card-title"><i class="ti ti-arrow-right"></i> Continue with</div>
      <div style="display:flex; gap:8px; flex-wrap:wrap;">
        <a href="/en/blacklist.php?ip=<?= urlencode($query) ?>" class="btn btn-primary" style="font-size:12px; padding:6px 14px;">
          <i class="ti ti-shield-search"></i> Blacklist Check
        </a>
        <a href="/en/asn.php?asn=<?= urlencode($geo['asn'] ?? '') ?>" class="btn btn-primary" style="font-size:12px; padding:6px 14px; background:#1e1a3a; border-color:#3d3080; color:var(--purple-dim);">
          <i class="ti ti-building"></i> ASN Lookup
        </a>
        <a href="/en/ping.php?host=<?= urlencode($query) ?>" class="btn btn-ghost" style="font-size:12px; padding:6px 14px;">
          <i class="ti ti-activity"></i> Ping Test
        </a>
      </div>
    </div>

  <?php endif; ?>

  <div class="ad-slot"><ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-8287576653347400" data-ad-slot="2715725452" data-ad-format="auto" data-full-width-responsive="true"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script></div>

  <div class="tools-row">
    <a href="/en/"               class="tool-btn"><i class="ti ti-home"></i><span>My IP</span></a>
    <a href="/en/ip-lookup.php"  class="tool-btn current"><i class="ti ti-world-search"></i><span>IP Lookup</span></a>
    <a href="/en/asn.php"        class="tool-btn"><i class="ti ti-building"></i><span>ASN Lookup</span></a>
    <a href="/en/blacklist.php"  class="tool-btn"><i class="ti ti-shield-search"></i><span>Blacklist</span></a>
    <a href="/en/dns-lookup.php" class="tool-btn"><i class="ti ti-search"></i><span>DNS Lookup</span></a>
  </div>

  <div class="seo-box">
    <h2>What is an IP Lookup?</h2>
    <p>An IP Lookup retrieves information about an IP address: the approximate geographic location, the Internet Service Provider (ISP), the ASN (Autonomous System Number) and the hostname. The location data comes from GeoIP databases and is accurate to city level – not street level. This tool is useful for identifying the origin of traffic, checking server locations or verifying network information.</p>
  </div>

<?php require_once __DIR__ . '/footer.php'; ?>
