Tags
xml , Sitemap
Asked 3 years ago
3 Sep 2021
Views 470
Guy

Guy posted

How to generate XML sitemap in PHP?

How to generate XML sitemap in PHP?
duglus

duglus
answered Sep 6 '21 00:00

follow the guideline for generating sitemap XML file :
below is simple sitemap.xml file :

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>http://www.a.com/b.html</loc>
    <lastmod>2021-06-04</lastmod>
  </url>
</urlset>


i am considering you have all link in database which need to include in sitemap.xml
suppose sitemap_urls have filed url and page upadte time , you can manage it by admin
so following code is for generating xml sitemap in PHP


//database connection 

$connection=mysqli_connect(...
..
..

$sitemapcontent='';
$sitemapcontent=$sitemapcontent.'<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
$result=mysqli_query("select * from sitemap_urls ",$connection);
while($siteslist=mysqli_fetch_arrray($result)){
$sitemapcontent=$sitemapcontent.'
  <url>
    <loc>'.$siteslist['url'].'</loc>
    <lastmod>'.$siteslist['update_date'].'</lastmod>
  </url>'; 
}
$sitemapcontent=$sitemapcontent.'
</urlset>'; 

echo $sitemapcontent;
Post Answer