Read page URL in XML? Or use PHP within an XML fil

2019-09-20 06:55发布

I have a flash element (I know, but please don't) with a settings.xml file.

One of the values in the XML file depends on the URL, so rather than having multiple instances of the flash folder, I want to do the following:

value="<?php $rest = substr(<?php echo curPageURL(); ?>, -2, -1) ?>"

I tried that in vain, but as you probably already knew, that won't work.

So I either need a way to write that in XML, or to allow that line of PHP within the XML file.

function curPageURL() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {
        $pageURL. = "s";
    }
    $pageURL. = "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL. = $ _SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL. = $ _SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

标签: php xml url
1条回答
冷血范
2楼-- · 2019-09-20 07:42

You need two things for this to work:

  1. Your server would need to be configured to parse XML files as PHP files. If you're using Apache, that's something in your configuration similar to this:

    AddHandler application/x-httpd-php5s .xml
    
  2. You'd need to use valid PHP syntax, like so:

    value="<?php echo substr( curPageURL(), -2, -1); ?>"
    

    Assuming that curPageURL() is a function defined in PHP.

Finally, you should ensure that the short_open_tag directive is set to Off in your php.ini configuration, or else PHP will throw syntax errors when it encounters XML opening tags, e.g. <?xml. This can be done in .htaccess with something similar to

php_value short_open_tag Off
查看更多
登录 后发表回答