Dieses PHP-Script führt eine umfassende SEO-Analyse einer beliebigen Webseite durch. Nach Eingabe einer URL überprüft das Tool verschiedene wichtige SEO-Faktoren und liefert übersichtliche Ergebnisse:
- Title: Ermittelt den Seitentitel aus dem HTML
<title>
-Tag. - Meta Description: Liest die Meta-Beschreibung aus dem HTML aus.
- H1 Überschrift: Gibt die erste H1-Überschrift der Seite aus.
- Anzahl H2 Überschriften: Zählt, wie viele H2-Überschriften auf der Seite vorhanden sind.
- Keywords (Meta Tag): Liest die Keywords aus dem Meta-Tag aus.
- Ladezeit (Response Time): Misst die Zeit in Millisekunden, die der Server für die Antwort benötigt.
- Mobile Friendly: Prüft, ob ein Meta-Viewport-Tag vorhanden ist, um die mobile Optimierung zu beurteilen.
- Robots.txt und Sitemap.xml: Prüft, ob diese wichtigen Dateien vorhanden sind.
- Bilder ohne ALT-Attribut: Zählt Bilder, die kein ALT-Attribut haben, was für Barrierefreiheit und SEO wichtig ist.
- HTTPS Nutzung: Prüft, ob die Seite über HTTPS ausgeliefert wird.
- Wortanzahl: Ermittelt die Anzahl der Wörter auf der Seite (Textinhalt).
- Canonical Tag: Prüft, ob ein Canonical-Link im Head gesetzt ist.
Zusätzlich führt das Script eine Keyword-Dichte Analyse durch, basierend auf den im Meta-Keywords-Tag angegebenen Begriffen. Dabei wird berechnet, wie oft ein Keyword im Text vorkommt im Verhältnis zur Gesamtwortzahl (in Prozent).
<?php
function getHttpResponseTime($url) {
$start = microtime(true);
$headers = @get_headers($url);
$end = microtime(true);
if(!$headers) return null;
return round(($end - $start) * 1000, 2);
}
function isMobileFriendly($url) {
$opts = [
"http" => [
"method" => "GET",
"header" => "User-Agent: Mozilla/5.0 (Linux; Android 10; Mobile) AppleWebKit/537.36 Chrome/86.0.4240.198 Safari/537.36\r\n"
]
];
$context = stream_context_create($opts);
$html = @file_get_contents($url, false, $context);
if(!$html) return "Nicht überprüfbar";
if (preg_match('/<meta name=["\']viewport["\'] content=["\'](.*?)["\']/si', $html)) {
return "Ja";
}
return "Nein";
}
function keywordDensity($text, $keyword) {
$text = strtolower(strip_tags($text));
$keyword = strtolower($keyword);
$words = str_word_count($text, 1);
$totalWords = count($words);
if ($totalWords == 0) return 0;
$count = 0;
foreach ($words as $word) {
if ($word == $keyword) $count++;
}
return round(($count / $totalWords) * 100, 2);
}
function checkFileExists($url) {
$headers = @get_headers($url);
if(!$headers) return false;
return strpos($headers[0], '200') !== false;
}
if(isset($_POST['url'])) {
$url = filter_var($_POST['url'], FILTER_VALIDATE_URL);
if(!$url) {
echo "Bitte eine gültige URL eingeben.";
exit;
}
$html = @file_get_contents($url);
if(!$html) {
echo "Die URL konnte nicht geladen werden.";
exit;
}
preg_match("/<title>(.*?)<\/title>/si", $html, $titleMatches);
$title = $titleMatches[1] ?? "Kein Title gefunden";
preg_match('/<meta name=["\']description["\'] content=["\'](.*?)["\']/si', $html, $descMatches);
$description = $descMatches[1] ?? "Keine Meta-Beschreibung gefunden";
preg_match('/<h1[^>]*>(.*?)<\/h1>/si', $html, $h1Matches);
$h1 = $h1Matches[1] ?? "Keine H1 Überschrift gefunden";
preg_match('/<meta name=["\']keywords["\'] content=["\'](.*?)["\']/si', $html, $keyMatches);
$keywords = $keyMatches[1] ?? "";
preg_match_all('/<h2[^>]*>(.*?)<\/h2>/si', $html, $h2Matches);
$h2count = count($h2Matches[0]);
$loadTime = getHttpResponseTime($url);
$mobileFriendly = isMobileFriendly($url);
$robotsUrl = rtrim($url, '/') . '/robots.txt';
$robotsExists = checkFileExists($robotsUrl) ? "Ja" : "Nein";
$sitemapUrl = rtrim($url, '/') . '/sitemap.xml';
$sitemapExists = checkFileExists($sitemapUrl) ? "Ja" : "Nein";
preg_match_all('/<img[^>]+>/i', $html, $imgTags);
$images = $imgTags[0];
$imagesWithoutAlt = 0;
foreach($images as $img) {
if (!preg_match('/alt=["\'].*?["\']/i', $img)) {
$imagesWithoutAlt++;
}
}
$isHttps = (parse_url($url, PHP_URL_SCHEME) === 'https') ? "Ja" : "Nein";
$textContent = strip_tags($html);
$wordCount = str_word_count($textContent);
preg_match('/<link rel=["\']canonical["\'] href=["\'](.*?)["\']\s*\/?>/si', $html, $canonicalMatches);
$canonical = $canonicalMatches[1] ?? "Kein Canonical Tag gefunden";
echo "<h2>Erweiterte SEO Analyse für: $url</h2>";
echo "<ul>";
echo "<li><strong>Title:</strong> " . htmlspecialchars($title) . "</li>";
echo "<li><strong>Meta Description:</strong> " . htmlspecialchars($description) . "</li>";
echo "<li><strong>H1 Überschrift:</strong> " . htmlspecialchars(strip_tags($h1)) . "</li>";
echo "<li><strong>Anzahl H2 Überschriften:</strong> " . $h2count . "</li>";
echo "<li><strong>Keywords (Meta Tag):</strong> " . htmlspecialchars($keywords ?: "Keine Keywords gefunden") . "</li>";
echo "<li><strong>Ladezeit:</strong> " . ($loadTime !== null ? $loadTime . " ms" : "Nicht messbar") . "</li>";
echo "<li><strong>Mobile Friendly:</strong> $mobileFriendly</li>";
echo "<li><strong>Robots.txt vorhanden:</strong> $robotsExists</li>";
echo "<li><strong>Sitemap.xml vorhanden:</strong> $sitemapExists</li>";
echo "<li><strong>Bilder ohne ALT-Attribut:</strong> $imagesWithoutAlt</li>";
echo "<li><strong>Seite mit HTTPS:</strong> $isHttps</li>";
echo "<li><strong>Wortanzahl auf der Seite:</strong> $wordCount</li>";
echo "<li><strong>Canonical Tag:</strong> " . htmlspecialchars($canonical) . "</li>";
echo "</ul>";
if(!empty($keywords)) {
$keyArray = explode(",", $keywords);
echo "<h3>Keyword-Dichte Analyse:</h3><ul>";
foreach($keyArray as $kw) {
$kw = trim($kw);
$density = keywordDensity($html, $kw);
echo "<li><strong>" . htmlspecialchars($kw) . ":</strong> " . $density . " %</li>";
}
echo "</ul>";
} else {
echo "<p>Keine Keywords für Keyword-Dichte Analyse gefunden.</p>";
}
} else {
?>
<form method="post">
<label>URL zur erweiterten SEO-Analyse:</label><br>
<input type="text" name="url" style="width:400px" placeholder="https://www.example.com" required>
<button type="submit">Analysieren</button>
</form>
<?php
}
?>