165 lines
7.8 KiB
PHP
165 lines
7.8 KiB
PHP
<?php
|
|
|
|
function analyze_seo($html) {
|
|
$results = [
|
|
'title' => ['text' => '', 'length' => 0, 'status' => 'danger'],
|
|
'description' => ['text' => '', 'length' => 0, 'status' => 'danger'],
|
|
'h1_count' => ['count' => 0, 'status' => 'danger'],
|
|
'alt_tags' => ['missing' => 0, 'total' => 0, 'status' => 'success'],
|
|
'og_tags' => ['found' => 0, 'status' => 'warning'],
|
|
];
|
|
|
|
if (empty($html)) return $results;
|
|
|
|
$doc = new DOMDocument();
|
|
@$doc->loadHTML($html);
|
|
|
|
// Title
|
|
$title_node = $doc->getElementsByTagName('title')->item(0);
|
|
if ($title_node) {
|
|
$results['title']['text'] = $title_node->nodeValue;
|
|
$results['title']['length'] = strlen($title_node->nodeValue);
|
|
if ($results['title']['length'] >= 10 && $results['title']['length'] <= 70) {
|
|
$results['title']['status'] = 'success';
|
|
}
|
|
}
|
|
|
|
// Meta Description
|
|
$metas = $doc->getElementsByTagName('meta');
|
|
foreach ($metas as $meta) {
|
|
if (strtolower($meta->getAttribute('name')) == 'description') {
|
|
$results['description']['text'] = $meta->getAttribute('content');
|
|
$results['description']['length'] = strlen($meta->getAttribute('content'));
|
|
if ($results['description']['length'] >= 50 && $results['description']['length'] <= 160) {
|
|
$results['description']['status'] = 'success';
|
|
}
|
|
break;
|
|
}
|
|
if (strpos(strtolower($meta->getAttribute('property')), 'og:') === 0) {
|
|
$results['og_tags']['found']++;
|
|
}
|
|
}
|
|
if ($results['og_tags']['found'] >= 3) { // og:title, og:description, og:image
|
|
$results['og_tags']['status'] = 'success';
|
|
}
|
|
|
|
// H1 Count
|
|
$h1s = $doc->getElementsByTagName('h1');
|
|
$results['h1_count']['count'] = $h1s->length;
|
|
if ($results['h1_count']['count'] === 1) {
|
|
$results['h1_count']['status'] = 'success';
|
|
}
|
|
|
|
// Alt Tags
|
|
$images = $doc->getElementsByTagName('img');
|
|
$results['alt_tags']['total'] = $images->length;
|
|
foreach ($images as $img) {
|
|
if (!$img->hasAttribute('alt') || trim($img->getAttribute('alt')) == '') {
|
|
$results['alt_tags']['missing']++;
|
|
}
|
|
}
|
|
if ($results['alt_tags']['missing'] > 0) {
|
|
$results['alt_tags']['status'] = 'warning';
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
$html_input = '';
|
|
$seo_results = null;
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['html_content'])) {
|
|
$html_input = $_POST['html_content'];
|
|
$seo_results = analyze_seo($html_input);
|
|
}
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>SEO Checker - Flatlogic LAMP Demo</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css">
|
|
</head>
|
|
<body>
|
|
|
|
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
|
|
<div class="container">
|
|
<a class="navbar-brand" href="index.php">Flatlogic<span style="color: #6366F1;">Vibes</span></a>
|
|
<div class="collapse navbar-collapse">
|
|
<ul class="navbar-nav ms-auto">
|
|
<li class="nav-item"><a class="nav-link" href="index.php">Home</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="vibe.php">Vibe</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="sandbox.php">Sandbox</a></li>
|
|
<li class="nav-item"><a class="nav-link active" href="seo.php">SEO</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="admin.php">Admin</a></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<main class="container my-5">
|
|
<div class="text-center mb-5">
|
|
<h1 class="display-5">SEO Checker</h1>
|
|
<p class="lead">Paste your HTML to get an on-page SEO analysis.</p>
|
|
</div>
|
|
|
|
<div class="card shadow-sm">
|
|
<div class="card-body">
|
|
<form action="seo.php" method="POST">
|
|
<div class="mb-3">
|
|
<label for="html_content" class="form-label">HTML Content</label>
|
|
<textarea class="form-control font-monospace" id="html_content" name="html_content" rows="12" placeholder="<html>..."><?php echo htmlspecialchars($html_input); ?></textarea>
|
|
</div>
|
|
<div class="text-center">
|
|
<button type="submit" class="btn btn-primary"><i class="bi bi-search"></i> Analyze</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if ($seo_results): ?>
|
|
<div class="card shadow-sm mt-5">
|
|
<div class="card-header">
|
|
<h2 class="h4 mb-0">Analysis Results</h2>
|
|
</div>
|
|
<div class="card-body">
|
|
<ul class="list-group list-group-flush">
|
|
<li class="list-group-item d-flex justify-content-between align-items-center">
|
|
<div><strong>Title Tag</strong> <small class="text-muted d-block"><?php echo htmlspecialchars($seo_results['title']['text']); ?> (<?php echo $seo_results['title']['length']; ?> chars)</small></div>
|
|
<span class="badge bg-<?php echo $seo_results['title']['status']; ?>"><?php echo $seo_results['title']['length'] > 0 ? 'Good' : 'Missing'; ?></span>
|
|
</li>
|
|
<li class="list-group-item d-flex justify-content-between align-items-center">
|
|
<div><strong>Meta Description</strong> <small class="text-muted d-block"><?php echo htmlspecialchars(substr($seo_results['description']['text'], 0, 100)); ?>... (<?php echo $seo_results['description']['length']; ?> chars)</small></div>
|
|
<span class="badge bg-<?php echo $seo_results['description']['status']; ?>"><?php echo $seo_results['description']['length'] > 0 ? 'Good' : 'Missing'; ?></span>
|
|
</li>
|
|
<li class="list-group-item d-flex justify-content-between align-items-center">
|
|
<div><strong>H1 Tags</strong> <small class="text-muted d-block">Should be exactly one.</small></div>
|
|
<span class="badge bg-<?php echo $seo_results['h1_count']['status']; ?>"><?php echo $seo_results['h1_count']['count']; ?> found</span>
|
|
</li>
|
|
<li class="list-group-item d-flex justify-content-between align-items-center">
|
|
<div><strong>Image Alt Tags</strong> <small class="text-muted d-block">All images should have descriptive alt text.</small></div>
|
|
<span class="badge bg-<?php echo $seo_results['alt_tags']['status']; ?>"><?php echo $seo_results['alt_tags']['missing']; ?> missing / <?php echo $seo_results['alt_tags']['total']; ?> total</span>
|
|
</li>
|
|
<li class="list-group-item d-flex justify-content-between align-items-center">
|
|
<div><strong>Open Graph Tags</strong> <small class="text-muted d-block">For social media sharing.</small></div>
|
|
<span class="badge bg-<?php echo $seo_results['og_tags']['status']; ?>"><?php echo $seo_results['og_tags']['found']; ?> found</span>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</main>
|
|
|
|
<footer class="py-4 mt-auto bg-light">
|
|
<div class="container text-center">
|
|
<p class="mb-0 text-muted">© <?php echo date("Y"); ?> Flatlogic. All rights reserved.</p>
|
|
</div>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|