🗣 Wd提问: 程序PHP站点地图
🤖 Ai回答: 我来为您提供一个完整的PHP站点地图生成程序,包含XML和HTML两种格式:
1、XML站点地图生成器
php
<?php
/
* XML站点地图生成器
*/
class XMLSitemapGenerator {
private $urls = [];
private $baseUrl;
public function __construct($baseUrl) {
$this-baseUrl = rtrim($baseUrl, '/');
}
/
* 添加URL到站点地图
*/
public function addUrl($loc, $lastmod = null, $changefreq = 'weekly', $priority = 0.8) {
$url = [
'loc' = $this-baseUrl 、$loc,
'lastmod' = $lastmod ?: date('Y-m-d'),
'changefreq' = $changefreq,
'priority' = $priority
];
$this-urls[] = $url;
}
/
* 从数据库获取动态内容
*/
public function addFromDatabase($pdo, $table, $baseUrl, $changefreq = 'weekly', $priority = 0.5) {
$stmt = $pdo-query(SELECT id, updated_at FROM {$table} WHERE status = 1 ORDER BY updated_at DESC);
while ($row = $stmt-fetch(PDO::FETCH_ASSOC)) {
$loc = $baseUrl 、$row['id'];
$lastmod = date('Y-m-d', strtotime($row['updated_at']));
$this-addUrl($loc, $lastmod, $changefreq, $priority);
}
}
/
* 生成XML站点地图
*/
public function generate() {
$xml = '<?xml version=1.0 encoding=UTF-8?' 、\n;
$xml .= '<urlset xmlns=http://www.sitemaps.org/schemas/sitemap/0.9' 、\n;
foreach ($this-urls as $url) {
$xml .= <url\n;
$xml .= <loc 、htmlspecialchars($url['loc']) 、</loc\n;
$xml .= <lastmod 、$url['lastmod'] 、</lastmod\n;
$xml .= <changefreq 、$url['changefreq'] 、</changefreq\n;
$xml .= <priority 、$url['priority'] 、</priority\n;
$xml .= </url\n;
}
$xml .= '</urlset';
return $xml;
}
/
* 保存站点地图到文件
*/
public function saveToFile($filename) {
$xml = $this-generate();
file_put_contents($filename, $xml);
return true;
}
}
2、HTML站点地图生成器
php
<?php
/
* HTML站点地图生成器
*/
class HTMLSitemapGenerator {
private $baseUrl;
private $categories = [];
public function __construct($baseUrl) {
$this-baseUrl = rtrim($baseUrl, '/');
}
/
* 添加分类和页面
*/
public function addCategory($category, $pages) {
$this-categories[$category] = $pages;
}
/
* 从数据库获取页面
*/
public function addPagesFromDatabase($pdo, $table, $category) {
$stmt = $pdo-query(SELECT title, slug, updated_at FROM {$table} WHERE status = 1 ORDER BY updated_at DESC);
$pages = [];
while ($row = $stmt-fetch(PDO::FETCH_ASSOC)) {
$pages[] = [
'title' = $row['title'],
'url' = $this-baseUrl 、'/' 、$row['slug'],
'updated_at' = $row['updated_at']
];
}
$this-addCategory($category, $pages);
}
/
* 生成HTML站点地图
*/
public function generate() {
$html = '<!DOCTYPE html';
$html .= '<html lang=zh-CN';
$html .= '<head';
$html .= '<meta charset=UTF-8';
$html .= '<meta name=viewport content=width=device-width, initial-scale=1.0';
$html .= '<title站点地图 ' 、$this-baseUrl 、'</title';
$html .= '<style';
$html .= $this-getCSS();
$html .= '</style';
$html .= '</head';
$html .= '<body';
$html .= '<div class=container';
$html .= '<h1站点地图</h1';
foreach ($this-categories as $category = $pages) {
$html .= '<div class=category';
$html .= '<h2' 、htmlspecialchars($category) 、'</h2';
$html .= '<ul class=sitemap-list';
foreach ($pages as $page) {
$html .= '<li class=sitemap-item';
$html .= '<a href=' 、htmlspecialchars($page['url']) 、'' 、htmlspecialchars($page['title']) 、'</a';
$html .= '<span class=updated-date(' 、date('Y-m-d', strtotime($page['updated_at'])) 、')</span';
$html .= '</li';
}
$html .= '</ul';
$html .= '</div';
}
$html .= '</div';
$html .= '</body';
$html .= '</html';
return $html;
}
/
* 获取CSS样式
*/
private function getCSS() {
return '
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: 333;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background-color: f5f5f5;
}
.container {
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: 2c3e50;
border-bottom: 2px solid 3498db;
padding-bottom: 10px;
}
h2 {
color: 34495e;
margin-top: 30px;
margin-bottom: 15px;
}
.sitemap-list {
list-style: none;
padding-left: 0;
}
.sitemap-item {
padding: 8px 0;
border-bottom: 1px solid eee;
}
.sitemap-item a {
text-decoration: none;
color: 3498db;
font-weight: 500;
}
.sitemap-item a:hover {
color: e74c3c;
}
.updated-date {
color: 7f8c8d;
font-size: 0.9em;
margin-left: 10px;
}
';
}
/
* 保存HTML站点地图到文件
*/
public function saveToFile($filename) {
$html = $this-generate();
file_put_contents($filename, $html);
return true;
}
}
3、自动化站点地图生成器
php
<?php
/
* 自动化站点地图生成器
*/
class AutoSitemapGenerator {
private $pdo;
private $baseUrl;
private $xmlGenerator;
private $htmlGenerator;
public function __construct($pdo, $baseUrl) {
$this-pdo = $pdo;
$this-baseUrl = $baseUrl;
$this-xmlGenerator = new XMLSitemapGenerator($baseUrl);
$this-htmlGenerator = new HTMLSitemapGenerator($baseUrl);
}
/
* 自动扫描网站目录生成站点地图
*/
public function autoGenerateFromDirectory($directory, $excludePatterns = []) {
$files = $this-scanDirectory($directory, $excludePatterns);
foreach ($files as $file) {
if (preg_match('/\.(php|html|htm)$/i', $file)) {
$relativePath = str_replace($directory, '', $file);
$url = str_replace('\\', '/', $relativePath);
if ($url === '/index.php' $url === '/index.html') {
$url = '/';
}
$mtime = filemtime($file);
$lastmod = date('Y-m-d', $mtime);
$this-xmlGenerator-addUrl($url, $lastmod, 'daily', 1.0);
}
}
}
/
* 扫描目录
*/
private function scanDirectory($directory, $excludePatterns) {
$files = [];
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory)
);
foreach ($iterator as $file) {
if ($file-isFile()) {
$path = $file-getPathname();
$shouldExclude = false;
foreach ($excludePatterns as $pattern) {
if (preg_match($pattern, $path)) {
$shouldExclude = true;
📣 商家广告 :
👍
0
👎
IP地址: 106.0.194.204
搜索次数: 1
提问时间: 2025-11-05 23:59:28
🛒 域名购买 :
6.hn.cn rdqq.cn 87.gold pai.show soukan.com.cn afuncard.com yxxg.cn mw.gs 67.plus sbnjt.com
❓️ 热门提问 :
批量未注册域名 域名抢注域名抢注 电梯主机分类 国外最大域名注册商 网站优化KPI 域名转让页面 国内最好的搜索引擎 淮安网站优化SEO分析工具 迪拜 vps cn域名估价 豌豆Ai站群搜索引擎系统
🌐 域名评估 :
s-y.com.cn 778.net.cn vtol.gz.cn su.gx.cn dd.jx.cn rh.cx oddk.cn zfxm.cn vivo.ln.cn afuncard.com
⛏ 最新挖掘 :
凯里酸汤鱼 贵阳烤脑花 都匀毛尖茶 花溪牛肉粉 贵阳烤生蚝 贵阳烤豆腐 贵阳纸包鱼 贵阳特色菜
🖌 热门作画 :
🤝 关于我们:
豌豆Ai
域名
建站
站群
留痕
推广
评估
源码
开发Ai
工具
日记
价格
加盟
广告
流量
留言
联系
🗨 加入群聊 :
🔗 友情链接 :
霸屏软件
二维码留言
ai提问
🧰 站长工具 :Ai工具 whois查询 搜索
📢 温馨提示 :本站所有问答由Ai自动创作,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
👉 技术支持 :本站由豌豆Ai 提供技术支持,使用的最新版:《豌豆Ai站群搜索引擎系统 V.25.10.25》搭建本站。