34107-vm/seo.php
Flatlogic Bot 4325b410a3 1.4
2025-09-16 16:47:58 +00:00

189 lines
9.0 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'] = trim($title_node->nodeValue);
$results['title']['length'] = strlen($results['title']['text']);
if ($results['title']['length'] >= 10 && $results['title']['length'] <= 70) {
$results['title']['status'] = 'success';
}
}
// Meta Description
$metas = $doc->getElementsByTagName('meta');
foreach ($metas as $meta) {
$name = strtolower($meta->getAttribute('name'));
$property = strtolower($meta->getAttribute('property'));
if ($name == 'description') {
$results['description']['text'] = trim($meta->getAttribute('content'));
$results['description']['length'] = strlen($results['description']['text']);
if ($results['description']['length'] >= 50 && $results['description']['length'] <= 160) {
$results['description']['status'] = 'success';
}
}
if (strpos($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']['total'] > 0 && $results['alt_tags']['missing'] > 0) {
$results['alt_tags']['status'] = 'warning';
}
return $results;
}
$url_input = '';
$seo_results = null;
$error_message = '';
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['url'])) {
$url_input = trim($_POST['url']);
if (filter_var($url_input, FILTER_VALIDATE_URL)) {
$context = stream_context_create([
'http' => [
'timeout' => 10, // 10 seconds
'user_agent' => 'FlatlogicSEOChecker/1.0',
]
]);
$html = @file_get_contents($url_input, false, $context);
if ($html) {
$seo_results = analyze_seo($html);
} else {
$error_message = "Could not retrieve content from the URL. Please check if the URL is correct and publicly accessible.";
}
} else {
$error_message = "Invalid URL provided. Please enter a valid URL (e.g., http://example.com).";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SEO Checker - 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.11.3/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body class="bg-light">
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
<div class="container">
<a class="navbar-brand" href="index.php">
<i class="bi bi-lightbulb-fill text-primary"></i>
LAMP Demo
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<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>
<li class="nav-item"><a class="nav-link" href="contact.php">Contact</a></li>
</ul>
</div>
</div>
</nav>
<main class="container my-5">
<div class="text-center mb-5">
<h1 class="display-5">Live SEO Checker</h1>
<p class="lead">Enter a URL to get an on-page SEO analysis.</p>
</div>
<div class="card shadow-sm">
<div class="card-body p-4">
<form action="seo.php" method="POST">
<div class="input-group mb-3">
<span class="input-group-text"><i class="bi bi-link-45deg"></i></span>
<input type="url" class="form-control form-control-lg" id="url" name="url" placeholder="https://example.com" value="<?php echo htmlspecialchars($url_input); ?>" required>
<button type="submit" class="btn btn-primary"><i class="bi bi-search"></i> Analyze</button>
</div>
</form>
<?php if ($error_message): ?>
<div class="alert alert-danger mt-3"><?php echo $error_message; ?></div>
<?php endif; ?>
</div>
</div>
<?php if ($seo_results): ?>
<div class="card shadow-sm mt-5">
<div class="card-header">
<h2 class="h4 mb-0">Analysis Results for <span class="text-primary"><?php echo htmlspecialchars($url_input); ?></span></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> <span class="badge bg-<?php echo $seo_results['title']['status']; ?>"><?php echo $seo_results['title']['length'] > 0 ? 'OK' : 'Missing'; ?></span><br><small class="text-muted"><?php echo htmlspecialchars($seo_results['title']['text']); ?> (<?php echo $seo_results['title']['length']; ?> chars)</small></div>
</li>
<li class="list-group-item d-flex justify-content-between align-items-center">
<div><strong>Meta Description</strong> <span class="badge bg-<?php echo $seo_results['description']['status']; ?>"><?php echo $seo_results['description']['length'] > 0 ? 'OK' : 'Missing'; ?></span><br><small class="text-muted"><?php echo htmlspecialchars(substr($seo_results['description']['text'], 0, 150)); ?>... (<?php echo $seo_results['description']['length']; ?> chars)</small></div>
</li>
<li class="list-group-item d-flex justify-content-between align-items-center">
<div><strong>H1 Tags</strong> <small class="text-muted">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">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">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="bg-white text-center text-muted py-4 mt-5 border-top">
<div class="container">
<p class="mb-0">&copy; <?php echo date("Y"); ?> LAMP Demo. 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>