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

$page_title   = 'Website Availability Check – Is a Website Down?';
$page_desc    = 'Free availability check: test if a website or server is reachable from Düsseldorf, Germany – HTTP status, load time and SSL check on ipcheck.tools.';
$page_current = 'erreichbarkeit';

$query   = trim(strip_tags($_GET['url'] ?? ''));
$result  = [];
$error   = '';

if ($query) {
    $url = $query;
    if (!preg_match('/^https?:\/\//i', $url)) $url = 'https://' . $url;

    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL            => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HEADER         => true,
        CURLOPT_NOBODY         => false,
        CURLOPT_TIMEOUT        => 10,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_MAXREDIRS      => 5,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_USERAGENT      => 'Mozilla/5.0 (compatible; ipcheck.tools/1.0)',
    ]);

    $start    = microtime(true);
    $response = curl_exec($ch);
    $time_ms  = round((microtime(true) - $start) * 1000);

    $http_code      = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $final_url      = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    $redirect_count = curl_getinfo($ch, CURLINFO_REDIRECT_COUNT);
    $ssl_verify     = curl_getinfo($ch, CURLINFO_SSL_VERIFYRESULT);
    $errno          = curl_errno($ch);
    $errmsg         = curl_error($ch);
    curl_close($ch);

    if ($errno) {
        $result = [
            'reachable' => false,
            'error'     => $errmsg,
            'time_ms'   => $time_ms,
        ];
    } else {
        $result = [
            'reachable'   => $http_code > 0 && $http_code < 500,
            'http_code'   => $http_code,
            'time_ms'     => $time_ms,
            'final_url'   => $final_url,
            'redirects'   => $redirect_count,
            'ssl_valid'   => $ssl_verify === 0,
            'uses_https'  => str_starts_with($final_url, 'https://'),
        ];
    }
}

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

<div class="hero">
  <div class="hero-label">Tool</div>
  <div class="hero-title">Availability Check</div>
  <div class="hero-sub">
    Check if a website is reachable · Location:
    <img src="https://flagcdn.com/20x15/de.png" width="20" height="15" alt="DE"
         style="vertical-align:middle; border-radius:2px; margin:0 3px;">
    Düsseldorf, Germany
  </div>
  <form method="GET" action="/en/erreichbarkeit.php">
    <div class="input-group">
      <input class="input-text" type="text" name="url"
             value="<?= h($query) ?>"
             placeholder="e.g. example.com or https://example.com"
             required />
      <button type="submit" class="btn btn-primary">
        <i class="ti ti-antenna"></i> Check
      </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 ($query && $result): ?>

    <div class="stat-grid">
      <div class="stat">
        <div class="stat-val" style="color:<?= $result['reachable'] ? 'var(--green)' : 'var(--red)' ?>;">
          <?= $result['reachable'] ? 'Reachable' : 'Down' ?>
        </div>
        <div class="stat-key">Status</div>
      </div>
      <div class="stat">
        <div class="stat-val" style="color:<?= isset($result['http_code']) ? ($result['http_code'] < 400 ? 'var(--green)' : 'var(--red)') : 'var(--text4)' ?>;">
          <?= $result['http_code'] ?? '–' ?>
        </div>
        <div class="stat-key">HTTP Status</div>
      </div>
      <div class="stat">
        <div class="stat-val" style="color:<?= $result['time_ms'] < 500 ? 'var(--green)' : ($result['time_ms'] < 1500 ? 'var(--orange)' : 'var(--red)') ?>;">
          <?= $result['time_ms'] ?> ms
        </div>
        <div class="stat-key">Load time</div>
      </div>
      <div class="stat">
        <div class="stat-val" style="color:<?= ($result['uses_https'] ?? false) ? 'var(--green)' : 'var(--orange)' ?>;">
          <?= ($result['uses_https'] ?? false) ? 'HTTPS' : 'HTTP' ?>
        </div>
        <div class="stat-key">Protocol</div>
      </div>
    </div>

    <?php if ($result['reachable']): ?>
    <div class="alert alert-success">
      <i class="ti ti-check"></i>
      <span><strong><?= h($query) ?></strong> is reachable from Düsseldorf, Germany. Response time: <?= $result['time_ms'] ?> ms.</span>
    </div>
    <?php else: ?>
    <div class="alert alert-danger">
      <i class="ti ti-alert-circle"></i>
      <span><strong><?= h($query) ?></strong> is not reachable. <?= isset($result['error']) ? h($result['error']) : '' ?></span>
    </div>
    <?php endif; ?>

    <div class="grid2">
      <div class="card card-<?= $result['reachable'] ? 'green' : 'blue' ?>">
        <div class="card-title"><i class="ti ti-antenna"></i> Availability Details</div>
        <div class="data-row"><span class="dk">URL</span><span class="dv mono" style="font-size:11px;"><?= h($query) ?></span></div>
        <?php if (isset($result['final_url']) && $result['final_url'] !== $query): ?>
        <div class="data-row"><span class="dk">Final URL</span><span class="dv mono" style="font-size:11px;"><?= h($result['final_url']) ?></span></div>
        <?php endif; ?>
        <div class="data-row"><span class="dk">HTTP Status</span>
          <span class="dv" style="color:<?= isset($result['http_code']) ? ($result['http_code'] < 400 ? 'var(--green)' : 'var(--red)') : 'var(--text4)' ?>;">
            <?= $result['http_code'] ?? '–' ?>
            <?php
            $codes = [200=>'OK',301=>'Moved Permanently',302=>'Found',304=>'Not Modified',400=>'Bad Request',401=>'Unauthorized',403=>'Forbidden',404=>'Not Found',500=>'Internal Server Error',503=>'Service Unavailable'];
            echo isset($codes[$result['http_code']]) ? ' – ' . $codes[$result['http_code']] : '';
            ?>
          </span>
        </div>
        <div class="data-row"><span class="dk">Load time</span><span class="dv mono"><?= $result['time_ms'] ?> ms</span></div>
        <?php if (isset($result['redirects'])): ?>
        <div class="data-row"><span class="dk">Redirects</span><span class="dv mono"><?= $result['redirects'] ?></span></div>
        <?php endif; ?>
        <div class="data-row"><span class="dk">HTTPS</span>
          <span class="dv"><?= ($result['uses_https'] ?? false) ? '<span class="badge badge-green">Yes</span>' : '<span class="badge badge-orange">No</span>' ?></span>
        </div>
      </div>

      <div class="card">
        <div class="card-title"><i class="ti ti-arrow-right"></i> Continue with</div>
        <div style="display:flex; flex-direction:column; gap:8px;">
          <a href="/en/ssl-check.php?host=<?= urlencode($query) ?>" class="btn btn-primary" style="font-size:12px; padding:6px 14px;">
            <i class="ti ti-lock"></i> SSL Check
          </a>
          <a href="/en/headers.php?url=<?= urlencode($query) ?>" class="btn btn-primary" style="font-size:12px; padding:6px 14px; background:#1e1a3a; border-color:#3d3080; color:var(--purple-dim);">
            <i class="ti ti-eye"></i> HTTP Headers
          </a>
          <a href="/en/dns-lookup.php?domain=<?= urlencode(preg_replace('/^https?:\/\//i','',$query)) ?>" class="btn btn-ghost" style="font-size:12px; padding:6px 14px;">
            <i class="ti ti-search"></i> DNS Lookup
          </a>
        </div>
      </div>
    </div>

  <?php else: ?>
    <div class="alert alert-info">
      <i class="ti ti-info-circle"></i>
      <span>Enter a URL or domain to check its availability from <strong>Düsseldorf, Germany</strong>.</span>
    </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/erreichbarkeit.php"  class="tool-btn current"><i class="ti ti-antenna"></i><span>Availability</span></a>
    <a href="/en/ssl-check.php"       class="tool-btn"><i class="ti ti-lock"></i><span>SSL Check</span></a>
    <a href="/en/ping.php"            class="tool-btn"><i class="ti ti-activity"></i><span>Ping Test</span></a>
    <a href="/en/port-check.php"      class="tool-btn"><i class="ti ti-plug"></i><span>Port Check</span></a>
  </div>

  <div class="seo-box">
    <h2>Website Availability Check</h2>
    <p>The availability check tests whether a website or server is reachable from our server in Düsseldorf, Germany. It measures the HTTP status code, load time and checks whether HTTPS is used. This is useful to determine whether a website is generally down or only unavailable for you locally. Note: this test checks reachability from Germany – local outages in other regions may not be detected.</p>
  </div>

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