PHP Help please?

+1 Gavin Lee · December 28, 2014
How would I get this to index pages that have no ending at all? My website is created of pages similar to this http://www.example.com/example/post-id-the-post-title-would-go-here and because it has no ending it does not display within the sitemap.

Config.php
<?php
define( 'SITEMAP_DIR', './' );
define( 'SITEMAP_DIR_URL', 'http://www.example.com' );
define( 'RECURSIVE', true );
$filetypes = array( 'php', 'html', 'pdf' );
// The replace array, this works as file => replacement, so 'index.php' => '', would make the index.php be listed as just /
$replace = array( 'index.php' => '' );
$xsl = 'xml.xsl';
$chfreq = 'daily';
$prio = 1;
$ignore = array( 'config.php' );
?>




Sitemap.php

<?php
require './config.php';

// Get the keys so we can check quickly
$replace_files = array_keys( $replace );

// Sent the correct header so browsers display properly, with or without XSL.
header( 'Content-Type: application/xml' );

echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";

$ignore = array_merge( $ignore, array( '.', '..', 'config.php', 'xml-sitemap.php' ) );

if ( isset( $xsl ) && !empty( $xsl ) )
echo '<?xml-stylesheet type="text/xsl" href="' . SITEMAP_DIR_URL . $xsl . '"?>' . "\n";

function parse_dir( $dir, $url ) {
global $ignore, $filetypes, $replace, $chfreq, $prio;

$handle = opendir( $dir );
while ( false !== ( $file = readdir( $handle ) ) ) {

    // Check if this file needs to be ignored, if so, skip it.
    if ( in_array( utf8_encode( $file ), $ignore ) )
        continue;

    if ( is_dir( $file ) ) {
        if ( defined( 'RECURSIVE' ) && RECURSIVE )
            parse_dir( $file, $url . $file . '/' );
    }

    // Check whether the file has on of the extensions allowed for this XML sitemap
    $fileinfo = pathinfo( $dir . $file );
    if ( in_array( $fileinfo['extension'], $filetypes ) ) {

        // Create a W3C valid date for use in the XML sitemap based on the file modification time
        if (filemtime( $dir .'/'. $file )==FALSE) {
            $mod = date( 'c', filectime( $dir . $file ) );
        } else {
            $mod = date( 'c', filemtime( $dir . $file ) );
        }

        // Replace the file with it's replacement from the settings, if needed.
        if ( in_array( $file, $replace ) )
            $file = $replace[$file];

        // Start creating the output
    ?>

    <url>
    <loc><?php echo $url . rawurlencode( $file ); ?></loc>
    <lastmod><?php echo $mod; ?></lastmod>
    <changefreq><?php echo $chfreq; ?></changefreq>
    <priority><?php echo $prio; ?></priority>
    </url><?php
    }
    }
closedir( $handle );
}
?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><?php
parse_dir( SITEMAP_DIR, SITEMAP_DIR_URL );
?>
</urlset>

I am not interested in rewriting the urls because I have already done this from the http://www.exaple.com/index.php?a=track&id=17 when I first started.

Post a Reply

Replies

Oldest  Newest  Rating
0 Raphael de Oliveira · January 2, 2015
@Gavin Murphy

Upload the broken XML (the one you've shown the picture of the error) on pastebin so we can take a look
0 Chris Sisco · December 28, 2014
Like I said I have no experience with sitemaps. From what I've read on this error though is that it's an error with your XML.

If I were trying to deal with this problem I would use the original Sitemap.php file that you posted above since it already mostly works and just add the code to it that will get the post information you need from the database.

...

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><?php
parse_dir( SITEMAP_DIR, SITEMAP_DIR_URL );

//I would put the code to pull post information from the database here.
/*
Create a loop and get the information needed from each post and echo in the same format that gets generated in the function that's already there.
<url>
<loc><?php echo $url . rawurlencode( $file ); ?></loc>
<lastmod><?php echo $mod; ?></lastmod>
<changefreq><?php echo $chfreq; ?></changefreq>
<priority><?php echo $prio; ?></priority>
</url>
*/

?>
</urlset>
0 Gavin Lee · December 28, 2014
@ChrisSisco

This is the error I am getting with this code that you provided in the url.
http://i.imgur.com/GdQUwKk.jpg

Not personally familiar with the error so I am a little lost not going to lie.
The line is this.

header('Content-type: application/xml');

Also within my config it looks a little something like this for database settings maybe something is wrong there but I am going to assume that it's connecting to the database as the error says nothing about a database connection.


<?php
$servername = "Host";
$username = "Username";
$password = "Password";
?>


Thanks in advance.
0 Chris Sisco · December 28, 2014
I've never messed around with sitemaps before but I did find something that might help you. IF your posts are stored in a database,

http://www.pontikis.net/blog/creating-dynamic-xml-sitemaps-using-php
  • 1

PHP

107,202 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators