Bladephp: Now with 8+ codeigniter, php, and laravel project 8+ php project From $37/Month From Rs. 2499/Month

How to Create Dynamic XML Sitemap in PHP

by
Category:php
php html
How to Create Dynamic XML Sitemap in PHP

Sitemaps prefer to be in XML format, but that does not mean that you have to write XML code for each page manually. Let's build a XML sitemap together with PHP. You may also like How to Read XML file Using PHP and How to Create RSS Feed For Website using PHP.

PHP Code

<? xml version = "1.0" encoding = "UTF-8" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>http://www.example.com/index.php </loc>
        <lastmod>2018-01-23T18:00:15+00:00</lastmod>
    </url>
</urlset>


<?php
header('Content-type: application/xml');
$get_result = mysqli_query($conn, "select * from post");

echo "<?xml version='1.0' encoding='UTF-8'?>" . "\n";
echo "<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>" . "\n";

echo "
<url>
 <loc>http://talkerscode.com/</loc>
 <lastmod>2016-01-23T18:00:15+00:00</lastmod>
 <changefreq>daily</changefreq>
</url>
<url>
 <loc>http://talkerscode.com/about.php</loc>
 <lastmod>2016-01-23T18:00:15+00:00</lastmod>
 <changefreq>daily</changefreq>
</url>
<url>
 <loc>http://talkerscode.com/contact.php</loc>
 <lastmod>2016-01-23T18:00:15+00:00</lastmod>
 <changefreq>daily</changefreq>
</url>";

while ($row = mysqli_fetch_array($get_result)) {
    echo "<url>";
    echo "<loc>" . $row['link'] . "</loc>";
    echo "<lastmod>" . $row['date'] . "</lastmod>";
    echo "<changefreq>daily</changefreq>";
    echo "</url>";
}

echo "</urlset>";
?>

0 Comments


Html code work in comment box


Related Post