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

$page_title   = 'HTTP Header Viewer – Analyze HTTP Response Headers';
$page_desc    = 'Free HTTP Header Viewer: analyze HTTP response headers of any website – security headers, redirects, server info and more on ipcheck.tools.';
$page_current = 'headers';

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

$security_headers = [
    'strict-transport-security' => ['name' => 'HSTS', 'desc' => 'Forces HTTPS connections', 'good' => true],
    'content-security-policy'   => ['name' => 'CSP', 'desc' => 'Prevents XSS attacks', 'good' => true],
    'x-frame-options'           => ['name' => 'X-Frame-Options', 'desc' => 'Prevents clickjacking', 'good' => true],
    'x-content-type-options'    => ['name' => 'X-Content-Type-Options', 'desc' => 'Prevents MIME sniffing', 'good' => true],
    'referrer-policy'           => ['name' => 'Referrer-Policy', 'desc' => 'Controls referrer information', 'good' => true],
    'permissions-policy'        => ['name' => 'Permissions-Policy', 'desc' => 'Controls browser features', 'good' => true],
];

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)',
    ]);

    $response    = curl_exec($ch);
    $http_code   = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $final_url   = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    $total_time  = round(curl_getinfo($ch, CURLINFO_TOTAL_TIME) * 1000);
    $redirect_count = curl_getinfo($ch, CURLINFO_REDIRECT_COUNT);
    curl_close($ch);

    if (!$response) {
        $error = "Could not connect to <strong>" . h($url) . "</strong>.";
    } else {
        $header_text = substr($response, 0, strpos($response, "\r\n\r\n") ?: strlen($response));
        $lines = explode("\r\n", $header_text);
        $status_line = array_shift($lines);

        foreach ($lines as $line) {
            if (strpos($line, ':') !== false) {
                [$key, $val] = explode(':', $line, 2);
                $results[strtolower(trim($key))] = trim($val);
            }
        }

        $results['_meta'] = [
            'status'    => $http_code,
            'final_url' => $final_url,
            'time_ms'   => $total_time,
            'redirects' => $redirect_count,
        ];
    }
}

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

<div class="hero">
  <div class="hero-label">Tool</div>
  <div class="hero-title">HTTP Header Viewer</div>
  <div class="hero-sub">Analyze HTTP response headers of any website</div>
  <form method="GET" action="/en/headers.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-eye"></i> Analyze
      </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 ($results && isset($results['_meta'])): ?>
    <?php $meta = $results['_meta']; unset($results['_meta']); ?>

    <div class="stat-grid">
      <div class="stat">
        <div class="stat-val" style="color:<?= $meta['status'] < 400 ? 'var(--green)' : 'var(--red)' ?>;"><?= $meta['status'] ?></div>
        <div class="stat-key">HTTP Status</div>
      </div>
      <div class="stat">
        <div class="stat-val"><?= $meta['time_ms'] ?> ms</div>
        <div class="stat-key">Load time</div>
      </div>
      <div class="stat">
        <div class="stat-val"><?= $meta['redirects'] ?></div>
        <div class="stat-key">Redirects</div>
      </div>
      <div class="stat">
        <div class="stat-val"><?= count($results) ?></div>
        <div class="stat-key">Headers</div>
      </div>
    </div>

    <!-- Security Headers Check -->
    <div class="card card-blue">
      <div class="card-title"><i class="ti ti-shield"></i> Security Headers</div>
      <?php foreach ($security_headers as $key => $info): ?>
      <div class="data-row">
        <span class="dk">
          <?= $info['name'] ?>
          <span style="font-size:10px; color:var(--text4); margin-left:4px;"><?= $info['desc'] ?></span>
        </span>
        <span class="dv">
          <?php if (isset($results[$key])): ?>
            <span class="badge badge-green">Present</span>
          <?php else: ?>
            <span class="badge badge-red">Missing</span>
          <?php endif; ?>
        </span>
      </div>
      <?php endforeach; ?>
    </div>

    <!-- All Headers -->
    <div class="card">
      <div class="card-title"><i class="ti ti-list"></i> All Response Headers (<?= count($results) ?>)</div>
      <table class="result-table">
        <thead><tr><th>Header</th><th>Value</th></tr></thead>
        <tbody>
          <?php foreach ($results as $key => $val): ?>
          <tr>
            <td class="mono" style="color:var(--blue-dim); font-size:11px; white-space:nowrap;"><?= h($key) ?></td>
            <td style="font-size:12px; word-break:break-all;"><?= h($val) ?></td>
          </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>

    <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/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/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>

  <?php else: ?>
    <div class="alert alert-info">
      <i class="ti ti-info-circle"></i>
      <span>Enter a URL or domain to analyze its HTTP response headers. Works with and without https://.</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/headers.php"   class="tool-btn current"><i class="ti ti-eye"></i><span>HTTP Headers</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/dns-lookup.php" class="tool-btn"><i class="ti ti-search"></i><span>DNS Lookup</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>What are HTTP headers?</h2>
    <p>HTTP headers are metadata sent between browser and server with every request and response. They control caching, content types, security policies and much more. Security headers like <strong>HSTS</strong>, <strong>CSP</strong> and <strong>X-Frame-Options</strong> protect against common attacks such as XSS and clickjacking. This tool shows all response headers and checks whether important security headers are present.</p>
  </div>

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