开发者

Trying to embed 3rd party Javascript that uses <?js= value ?>, how can i stop php from parsing the "<?"

I'm working in an insane mess of a CMS system where php, javascript, and html are all within templates. I'm trying to drop in code from a third party site for integration functionality, but they have javascript of the form:

<script type="lib">
Hello, <?js= firstName ?&开发者_运维问答gt;!
</script>

The PHP interpreter see's and does its thing, and thus the entire template errors out.

Anyone know how to perhaps wrap this code block to prevent PHP interpreter from caring about it?

Thanks in advance!


If disabling PHP Short-tags isn't an option, you could simply output said string using PHP, so you'd get something like this:

Hello, <?php print "<?js= firstName ?>"; ?>!


Disable PHP Short-Tags and you should not have an issue.


In a PHP document with short open tags enabled, the PHP interpreter will see the <? part of the <?js tag and will try to parse it as PHP, resulting in this error:

Parse error: syntax error, unexpected T_STRING in ... on line ...

To fix the problem, disable the short form (<? ?>) of PHP's open tag.

This will now disable the use of <? and <?= in your PHP scripts. If your site only uses <?php tags then this is safe to do; if it uses <? as well then it is not safe to do as your scripts will no longer function as expected.

There are two ways your Web Host or System Administration will have configured Apache to use PHP:

  • Apache loads the PHP interpreter as an Apache module
  • Apache runs the PHP interpreter as a CGI binary

Which solution you will use below will depend on how Apache is running PHP in your environment*.

In Apache's .htaccess file, place the following:

# PHP as an Apache Module
php_value short_open_tag 0

In the global or a local php.ini file, place the following:

# PHP as a CGI Wrapper
short_open_tag = Off

In either case, you'll want to restart Apache after making any configuration changes to ensure your new settings get picked up.


Note: If your server is configured to run PHP as an Apache module, then you will have the choice of using either a php.ini or Apache .htaccess files. However, if your server runs PHP as a CGI wrapper then you will only have the choice of using php.ini files locally to change settings, as Apache is no longer in complete control of PHP.

When PHP is running as a CGI wrapper, putting PHP settings in to an .htaccess file will cause the server throw an error.


Do you happen to be running apache? Do these files live separate from the PHP? If so, you could use an .htaccess file (or httpd.conf if you have the rights to modify it) to do something like this:

<FilesMatch "\.template_file_ext">
    php_flag engine off
</FilesMatch>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜