Avatar

Sitemap Generator (Features)

by Mardor ⌂, Tuesday, March 13, 2018, 13:35 (2233 days ago)

I just wrote a little PHP script to generate a Google sitemap from mlf database entries. As I am neither a good PHP programmer nor a MySQL expert there might be plenty room for enhancements.

Just save the following code as sitemap_xml.php and put it into the root folder of your forum:

<?php
// Erzeugt eine Google-Sitemap für Mein Kleines Forum
 
header("Content-Type: text/xml; charset: UTF-8");
 
define('IN_INDEX', true);
include('config/db_settings.php');
include('includes/functions.inc.php');
include('includes/main.inc.php');
 
function schreibe_eintrag($datum, $url, $freq, $prio)
{
echo "<url>\n <loc>$url</loc>\n <lastmod>$datum</lastmod>\n <changefreq>$freq</changefreq>\n <priority>$prio</priority>\n</url>\n";
}
 
$result=mysqli_query($connid, "SELECT id, pid, DATE_FORMAT(time,'%Y-%m-%d') AS datum, DATE_FORMAT(last_reply,'%Y-%m-%d') AS la_datum FROM ".$db_settings['forum_table']." ORDER BY id DESC");
if(!$result) die('Datenbankfehler!');
 
echo "<?xml version='1.0' encoding='UTF-8'?>\n<urlset xmlns='https://www.sitemaps.org/schemas/sitemap/0.9'>\n";
 
// Erste Zeile holen, um maxid zu erfahren
$zeile = mysqli_fetch_array($result);
 
schreibe_eintrag($zeile["datum"],$settings['forum_address'],"daily",1);
schreibe_eintrag($zeile["datum"],$settings['forum_address']."index.php?id=".$zeile["id"],"daily",1);
$maxid=$zeile["id"];
 
// Restliche Einträge lesen und Priorität proportional verringern
while ($zeile = mysqli_fetch_array($result)) 
{
$prio=round($zeile["id"]/$maxid,2); 
schreibe_eintrag($zeile["datum"],$settings['forum_address']."index.php?id=".$zeile["id"],"weekly",$prio);
}
 
echo "</urlset>\n";
 
mysqli_free_result($result);
?>

Sitemap Generator

by Tooter, Tuesday, March 20, 2018, 12:09 (2226 days ago) @ Mardor

Is this OK to use, or are there errors? Dont want to lose Google!

RSS Feed of thread