How to Generate Sitemap in PHP ? Dynamic sitemap Generator in PHP

In this tutorial im going to learn how to generate Sitemap. In this tutorial im going to share how to generate dynamic sitemap using php.

first create below file


Next go to index.php file and put below code.

<!DOCTYPE html>
<html lang="en">

    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src=""></script>
    <script src=""></script>
    <title>Site Map Generator</title>


<body bgcolor="#D5C826">

    <div class="loader">
        <img src="loading.gif" alt="Loading.../">

        <marquee behavior="" direction=""> Site Map Generator </marquee>
        <img src="img/loader.gif" id="gif" style="display: block; margin: 0 auto; width: 100px; visibility: hidden;">
            <div id="pageloader">
                <img src="abc.gif" alt="processing..." />
            <form method="POST" action="generate_sitemap.php">
        <label for="website_url">Website URL:</label>
        <input type="text" id="website_url" name="website_url" required>
        <button type="submit">Generate Sitemap</button>
<!-- page reload loader by amit  -->
<script type="text/javascript">
    window.addEventListener("load", function () {
        const loader = document.querySelector(".loader");
        loader.className += " hidden";

    $(document).ready(function () {
        $("#myform").on("submit", function () {
        }); //submit
    }); //document ready
<!-- page loader by amit  -->


Next to create below file


Next go to generate_sitemap.php file and put below code.


function generateSitemapXML($url)
    $xml = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
    $xml .= '<urlset xmlns="">' . PHP_EOL;

    // Add the homepage URL
    $xml .= '<url>' . PHP_EOL;
    $xml .= '<loc>' . $url . '</loc>' . PHP_EOL;
    $xml .= '<changefreq>daily</changefreq>' . PHP_EOL;
    $xml .= '<priority>1.0</priority>' . PHP_EOL;
    $xml .= '</url>' . PHP_EOL;

    // Add other pages to the sitemap
    $pages = [
        // Add more pages here

    foreach ($pages as $page) {
        $pageUrl = rtrim($url, '/') . $page;
        $xml .= '<url>' . PHP_EOL;
        $xml .= '<loc>' . $pageUrl . '</loc>' . PHP_EOL;
        $xml .= '<changefreq>weekly</changefreq>' . PHP_EOL;
        $xml .= '<priority>0.8</priority>' . PHP_EOL;
        $xml .= '</url>' . PHP_EOL;

    $xml .= '</urlset>' . PHP_EOL;

    return $xml;

// Get the website URL from the form submission or set a default value
$websiteUrl = isset($_POST['website_url']) ? $_POST['website_url'] : '';

// Generate the sitemap XML
$sitemapXml = generateSitemapXML($websiteUrl);

// Set the appropriate headers for download
header('Content-Type: application/xml');
header('Content-Disposition: attachment; filename="sitemap.xml"');

// Output the sitemap XML
echo $sitemapXml;

Now run the file


Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.

Related Posts

How to Upload Big Database using Command in PhpMyAdmin ?

If you need to upload a large database to phpMyAdmin using a command-line interface, you can use the mysql command to import the database dump file directly…

Error: error:0308010C:digital envelope routines::unsupported

In this tutorial I’m going to share how to solve Error: error:0308010C:digital envelope routines::unsupported. Just copy below code and run your terminal. After run above all code…

API calls from the server require an appsecret_proof argument

In this tutorial im going to solve this issue API calls from the server require an appsecret_proof argument. 1st step go to developer facebook advance setting and…

Example of Inharitance in PHP ?

In this tutorial we’re going to learn how to use inharitance in php with example. Inheritance is a way to create a new class that is a…

How to print first 50 value and last 10 data in php

In this tutorial we’re going to share how to print first 50 value of data and the last value of data To print the first 50 values…

How to count Using substr_count function in PHP ?

In this tutorial we’re going to learn how to use substr_count function in php with example. 1step create below file And put below code Output:-

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x