为什么PHP文件不用于(自定义)CSS和JS? [英] Why aren't PHP files used for (custom) CSS and JS?

查看:137
本文介绍了为什么PHP文件不用于(自定义)CSS和JS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,已经在我的头上徘徊了几天。

I have a question that has been hovering in my head for a few days.

为什么人们不做 <$ c c>

添加<?php header(Content-type: text / javascript; charset:UTF-8); ?> 到浏览器可读,你可以通过设置 Content-type 属性来对css文件进行同样的操作 text / css

Adding <?php header("Content-type: text/javascript; charset: UTF-8"); ?> to the file makes it readable by browsers, and you can do the same thing to css files by setting the Content-type property to text/css.

它可以将PHP和方法的所有变量用于其他语言。例如,根据css中的用户首选项更改主题主色,或预加载您的javascript可以在文档加载时使用的数据。

It lets you use all the variables of PHP and methods into the other languages. Letting you, as an example, change the theme main colors depending on user preferences in css, or preloading data that your javascript can use on document load.

有不好的一面使用这种技术?

Are there bad sides of using this technique?

预先感谢您的时间,每天都有美好的一天。

Thank you in advance for your time, and have a nice day everyone.

推荐答案

人们比你想象的更频繁。你只是不能看到它,因为通常这种技术与URL重写结合使用,这意味着浏览器不能告诉静态服务的.css文件和由PHP脚本生成的动态样式表之间的区别。

People do it more often than you think. You just don't get to see it, because usually this technique is used in combination with URL rewriting, which means the browser can't tell the difference between a statically-served .css file and a dynamic stylesheet generated by a PHP script.

但是,有几个强烈的原因不是

However, there are a few strong reasons not to do it:


  • 在默认配置中,Apache将PHP脚本输出视为随时更改,并设置适当的标头以防止缓存(否则,动态内容将无法正常工作)。但是,这意味着浏览器不会缓存您的CSS和JavaScript,这是坏的 - 他们将重新加载通过网络为每个页面加载

  • CSS和Javascript一旦部署,就很少更改

  • 运行PHP脚本(即使只是启动解释器)比运行静态文件更昂贵,因此您应该避免它,除非绝对必要。

  • 很难确保您输出的Javascript是正确和安全的; Javascript的转义动态值并不像你想象的那么简单,如果这些值是用户提供的,那么就是要求麻烦。

  • In a default configuration, Apache treats PHP script output as 'subject to change at any given time', and sets appropriate headers to prevent caching (otherwise, dynamic content wouldn't really work). This, however, means that the browser won't cache your CSS and javascript, which is bad - they'll be reloaded over the network for every single page load. If you have a few hundred page loads per second, this stuff absolutely matters, and even if you don't, the page's responsivity suffers considerably.
  • CSS and Javascript, once deployed, rarely changes, and reasons to make it dynamic are really rare.
  • Running a PHP script (even if it's just to start up the interpreter) is more expensive than just serving a static file, so you should avoid it unless absolutely necessary.
  • It's pretty damn hard to make sure the Javascript you output is correct and secure; escaping dynamic values for Javascript isn't as trivial as you'd think, and if those values are user-supplied, you are asking for trouble.

还有一些更容易设置的替代方法:

And there are a few alternatives that are easier to set up:


  • 编写几个样式表并动态选择正确的样式表。

  • 根据类名称制作样式表规则,并在HTML中动态设置。

  • 对于javascript,在父文档中定义动态零件包括静态脚本。最典型的场景是在文档中设置一些全局变量,并在静态脚本中引用它们。

  • 将动态脚本编译为静态文件,作为构建/部署过程的一部分。这样,您就可以在CSS中获得PHP的安全性,但您仍然可以提供静态文件。

  • Write a few stylesheets and select the right one dynamically.
  • Make stylesheet rules based on class names, and set those dynamically in your HTML.
  • For javascript, define the dynamic parts inside the parent document before including the static script. The most typical scenario is setting a few global variables inside the document and referencing them in the static script.
  • Compile dynamic scripts into static files as part of the build / deployment process. This way, you get the comfort of PHP inside your CSS, but you still get to serve static files.

PHP动态生成CSS:

If you want to use PHP to generate CSS dynamically after all:


  • 覆盖缓存头,允许浏览器和代理缓存它们。您甚至可以将缓存过期设置为永不,并添加一个伪造的查询字符串参数(例如< link rel =stylesheettype =text / csshref =http:// example .com / stylesheet.css?dummy = 121748283923> ),并在脚本更改时更改它:浏览器会将此解释为不同的网址并跳过缓存版本。

  • 设置URL重写,以便脚本的URL具有.css扩展名:某些浏览器(IE)在扩展名不匹配的情况下使MIME类型错误是众所周知的,尽管正确的 Content-Type 标头。

  • Override the caching headers to allow browsers and proxies to cache them. You can even set the cache expiration to 'never', and add a bogus query string parameter (e.g. <link rel="stylesheet" type="text/css" href="http://example.com/stylesheet.css?dummy=121748283923">) and change it whenever the script changes: browsers will interpret this as a different URL and skip the cached version.
  • Set up URL rewriting so that the script's URL has a .css extension: some browsers (IE) are notorious for getting the MIME type wrong under some circumstances when the extension doesn't match, despite correct Content-Type headers.

这篇关于为什么PHP文件不用于(自定义)CSS和JS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆