Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP throwing error on XML opening tag

I have a file that is including a file named sitemap.html and in the sitemap, I have the following code:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <url>
        <loc><?php echo SITE_URL . '/'; ?></loc>
        <changefreq>monthly</changefreq>
        <priority>1.00</priority>
    </url>

    <?php foreach ($this->data->content as $content): ?>
        <url>
            <loc><?php echo SITE_URL . $content->permalink; ?></loc>
            <lastmod><?php echo date("Y-m-d", $content->publish_date); ?></lastmod>
            <changefreq>monthly</changefreq>
            <priority><?php echo $content->sitemap_index; ?></priority>
        </url>
    <?php endforeach; ?>
</urlset>

Everything seems okay, but I'm getting an error 500 that's saying:

PHP Parse error: syntax error, unexpected 'version' (T_STRING) in sitemap.html on line 1

As you can see, I'm using <?xml and not <?php so why is it trying to parse it as PHP?

At first, I thought it was the magic quotes that are causing an issue, but when I do a var_dump of get_magic_quotes_gpc(), I get bool(false) so the magic quotes aren't even on.

Also, in my php.ini, I see that magic_quotes is OFF.

As an alternative to fix it, I replaced my first line with the following:

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>

But I'm still curious about what's happening.

I have other websites hosted on the same server (PHP version 5.4.45), and I don't get the PHP Parse Error on the sitemap of the other websites...

Any idea what might be causing this error?

like image 257
Chin Leung Avatar asked Oct 13 '16 19:10

Chin Leung


3 Answers

You should check short_open_tag option. The way i see it, PHP sees <? part of your <?xml as a php open tag and generates an error.

like image 85
Eternal1 Avatar answered Oct 03 '22 09:10

Eternal1


If you're going to have PHP process HTML pages and it's working on every other site, then you're (obviously) dealing with a difficult-to-troubleshoot configuration issue.

You can either continue searching for the short_open_tags setting buried somewhere on the server or you could simply echo the XML declaration using PHP.

<?php echo '<?xml version="1.0" encoding="UTF-8" ?>' ?>
<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">
    <url>
        <loc><?php echo SITE_URL . '/'; ?></loc>
        <changefreq>monthly</changefreq>
        <priority>1.00</priority>
    </url>

    <?php foreach ($this->data->content as $content): ?>
        <url>
            <loc><?php echo SITE_URL . $content->permalink; ?></loc>
            <lastmod><?php echo date("Y-m-d", $content->publish_date); ?></lastmod>
            <changefreq>monthly</changefreq>
            <priority><?php echo $content->sitemap_index; ?></priority>
        </url>
    <?php endforeach; ?>
</urlset>

Here's an older page on the topic: PHP opening tags and XML declaration

I prefer to give all PHP code files the .php extension so it's abundantly clear that they contain code and not incur the overhead of having PHP process every HTML file, but that's just my opinion.

like image 40
Dan Wilson Avatar answered Oct 03 '22 09:10

Dan Wilson


As mentioned, this is due to the short_open_tag option

Tells PHP whether the short form (<? ?>) of PHP's open tag should be allowed. If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?> inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version="1.0"?>'; ?>. Also, if disabled, you must use the long form of the PHP open tag (<?php ?>).

like image 42
Machavity Avatar answered Oct 03 '22 09:10

Machavity