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

index.php

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

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

<head>
    <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="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <title>Site Map Generator</title>

</head>

<body bgcolor="#D5C826">


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

    <h1>
        <marquee behavior="" direction=""> Site Map Generator </marquee>
    </h1>
    <div>
        <img src="img/loader.gif" id="gif" style="display: block; margin: 0 auto; width: 100px; visibility: hidden;">
        <center>
            <div id="pageloader">
                <img src="abc.gif" alt="processing..." />
            </div>
            <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>
    </form>
    </div>
</body>
<!-- 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 () {
            $("#pageloader").fadeIn();
        }); //submit
    }); //document ready
</script>
<!-- page loader by amit  -->

</html>

Next to create below file

generate_sitemap.php

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

<?php

function generateSitemapXML($url)
{
    $xml = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
    $xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . 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 = [
        '/about',
        '/contact',
        '/products',
        // 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'] : 'https://example.com';

// 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

Output:-

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 Generate Instagram Graph Api Access token ?

In this tutorial im going to generate instagram access token in very easy after reading this tutorial you’ll able to generate access token. 1st step Login Facebook…

Restrict PHP Information Leakage for Website Security ?

Restricting PHP information leakage is crucial for enhancing the security of PHP-based applications. By minimizing the amount of information exposed to potential attackers, you can reduce the…

Top Performance Checklist for PHP ?

In this tutorial we’re going to learn what are the checklist for improve php website performance, so follow this tutorial for improve website performance. 1 Update PHP…

Difference between md5 password and Hash Passowrd Algorithm ?

The main differences between MD5 and modern password hashing algorithms like Bcrypt (which is often used through Hash::make() in Laravel) lie in their security properties: Example: –…

How to print Multidimensional array using JavaScript ?

What is Multidimensional Array ? A multidimensional array is a data structure that can hold values organized in multiple dimensions or levels. In its simplest form, a…

How to remove @gmail.com from response data.

In this tutorial i’m going to learn how to remove @gmail.com from whatever coming in response data. One way to remove @gmail.com from response data is by…

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