32,000+ SUBSCRIBERS CAN'T GO WRONG !!!

phpans
GET TOP ARTICLES, SNIPPETS, SCRIPTS IN YOUR INBOX FOR FREE!

Delivered by GET EMAIL UPDATES FeedBurner
We always respect your privacy and take protecting it seriously.
We won't Spam You

Create XML Sitemap Using PHP and MySQL

, 0,
Create XML Sitemap Using PHP and MySQL
This tutorial is about Create XML Sitemap Using PHP and MySQL. A sitemap is important for a website to inform search engine to index web page. A sitemap helps search engines for crawling web pages that case sitemap is very important for a website. Webmaster should update website's search engines regularly for indexing new pages. There are a lot of sitemap creator tools in online and these sitemap creator tools create a static sitemap for you. If you use static sitemap then you should update this regularly and add new updates every time but this a pretty hard to do. If you use dynamic sitemap for your website then this will automatically update with every new update. This tutorial helps you to create a dynamic sitemap using PHP and MySQL. If you want to make this follow step by step instructions.

Step-1. Create Database Table For Sitemap Feed


If you haven't any database to make sitemap then create instantly then create a database table for it. If you have already a database and table and if you want to make sitemap based on this database table then you can skip this step. For new table insert following command bellow.
CREATE TABLE IF NOT EXISTS articles (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) DEFAULT NULL,
link VARCHAR(255) NULL,
date DATE DEFAULT NULL,
description TEXT DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM;

The database table is ready for use. Insert some data into this table. Then follow the second step. This step will help us to create all functions for the sitemap.

Step-2. Create Functions For Sitemap


Create a PHP file for put all necessary functions. We have created a file name as sitemap.class.php . Create your own and put down this code bellow.
sitemap.class.php
<?php
class sitemap
{
public $conn;
public function __construct()
{
if(!$this->conn())
{
die('Failed to connect with MySQL');
self::close();
}
}

public function conn()
{
$host = "localhost";
$user = "root";
$pass = "";
$name = "test";
$conn = mysqli_connect($host,$user,$pass,$name);
if (mysqli_connect_errno())
{
die("Failed to connect with MySQL: ".mysqli_connect_error());
}
else
{
return $this->conn = $conn;
}


}
public function head()
{
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
}
public function feed($url,$freq)
{
echo '
<url>
<loc>'.$url.'</loc>
<changefreq>'.$freq.'</changefreq>
</url>
';
}
public function foot()
{
echo '</urlset>';
}
public function clean($string) {
$string = strtolower( preg_replace('@[\W_]+@', '-', $string) );
$string = rtrim($string,'-');
$string = strtolower($string);
return $string;
}
public function pages($pages)
{
$allpage = explode(',',$pages);
foreach ($allpage as $page)
{
$link = 'http://'.$_SERVER['HTTP_HOST'].$page;
self::feed($link,'monthly');
}
}
public function close()
{
mysqli_close(self::conn());
}
}

?>

Our full functions are ready. You should config it as yours own for connecting with the database. These functions help you to execute full sitemap. Now we are going to the main step where XML sitemap will appear.

Step-3. Display Sitemap in PHP


Create a PHP file name as sitemap.php and write down this code bellow.
sitemap.php
<?php
header('Content-type: text/xml');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
date_default_timezone_set('UTC');
define('slash','/');
define('m','monthly');
define('a','always');
define('w','weekly');
define('d','daily');
$url = 'http://'.$_SERVER['HTTP_HOST'];
include_once 'sitemap.class.php';
$sitemap = new sitemap;
$sitemap->head();
$sitemap->feed($url,m);

$query = "SELECT * FROM articles ORDER BY id";
if ($result = mysqli_query($sitemap->conn(), $query)) {
while ($row = mysqli_fetch_assoc($result)) {
$link = $url.slash.sitemap::clean($row['name']);
sitemap::feed($link,a);
}
mysqli_free_result($result);
}
$pages = '/about-us,/contact-us,/privacy-policy';
sitemap::pages($pages);
$sitemap->foot();
$sitemap->close();
?>

Our sitemap is fully ready. If you added any data into articles table then this data will show in sitemap. You can add custom pages if you want. Just chage $pages value to your custo pages. You can able to see this sitemap normally browseing this file. Example like - http://example.com/sitemap.php If you put sitemap.php file in root directory then this PHP file will show you xml sitemap. But we want to visble our sitemap to an xml file extension. So, we have to do a simple task. This task help you to visble sitemap.php to sitemap.xml. Yes, a simple .htaccess tricks can make this for you.

Step-4. .htaccess tricks for xml sitemap


Edit your htaccess file and append this code into your website's .htaccess file.
RewriteEngine On
RewriteRule ^sitemap.xml$ sitemap.php [NC,L]

This htaccess code rewrite a rule for visble sitemap.php as sitemap.xml. You can do many interesting things with .htaccess . You may like 15 useful htaccess tips and tricks. Here you will get some awesome .htaccess tips and tricks. After completing this task you standerd sitemap is fully ready for you and now can browse as http://example.com/sitemap.xml . This will show your dynamic sitemap data. Finally our sitemap has completed.

I hope you understand this. I have attached this full project with this article. You are free to use this for your project. If you have any question or comment about this XML Sitemap Using PHP and MySQL article please don't hesitate to do that, please comment your question in comment section.

Download PDF
Signup & Get $25 Free
Is this article helpful for you?





captcha

  • Connect
  •       
Top
Create an free account and start learning & sharing! Sign Up