gif文件头欺骗_欺骗一个PHP文件以使其认为是样式表

gif文件头欺骗

On occasion, it is easier to produce dynamically with a language like than to keep styles in a static file. Reasons for doing so might include a desire to automatically generate CSS3 prefixes to simplify the CSS code, or customizing our site styles in response to the date or season.

有时候,用类的语言动态生成比将样式保存在静态文件中容易。 这样做的原因可能包括希望自动生成CSS3前缀以简化CSS代码,或者根据日期或季节自定义我们的网站样式。

We can't use PHP in any file that does not have a .php extension… so how to we tell our stylesheet that it is, in fact, PHP? By doing something that may appear a little counter-intuitive: linking our pages to a PHP page as a stylesheet.

我们不能在任何不具有.php扩展名的文件中使用PHP。因此,如何告诉样式表它实际上是PHP? 通过做一些看起来违反直觉的事情:将我们的页面作为样式表链接到PHP页面。

<link rel="stylesheet" href="styles.php">

When we do this, we have to take two other steps. The first is to convince styles.php that (in regards to other pages) it is not PHP, but CSS.

当我们这样做时,我们必须采取另外两个步骤。 首先是要说服styles.php (就其他页面而言)不是PHP,而是CSS。

A PHP function that I’ve looked at in the past is header(). Previously, I used it to redirect a link; now, we are going to use it to fool the browser into believing that styles.php is actually a CSS file. As the very first line in styles.php, write:

我过去看过PHP函数是header() 。 以前,我用它来重定向链接。 现在,我们将使用它来欺骗浏览器,使其认为styles.php实际上是CSS文件。 作为styles.php的第一行,编写:

<?php header(‘Content-type: text/css’); ?>

That’s step number one. The browser will now take any CSS we provide on that page, including any CSS that is generated by PHP, as if it were real style declarations. For example, as the next line in styles.php you could write:

那是第一步。 现在,浏览器将使用我们在该页面上提供的任何CSS,包括PHP生成的任何CSS,就好像它们是真实样式声明一样。 例如,作为styles.php中的下一行,您可以编写:

<?php echo "* { border: none; }"; ?>

It’s important to remember that we can do all the scripting we like on this page, but the final output that the browser sees must be CSS… ideally, valid CSS. Obviously, this partial CSS reset is only a simple example: we'd use the PHP abilities of our stylesheet for far more practical needs, such as the scenarios given above.

重要的是要记住,我们可以完成此页面上喜欢的所有脚本,但是浏览器看到的最终输出必须是CSS…理想情况下是有效CSS。 显然,部分CSS重置只是一个简单的示例:我们将样式表PHP功能用于更实际的需求,例如上述场景。

The final step may be necessary depending on your hosting provider: you also need to tell the server to deliver styles.php as a stylesheet. In the .htaccess file for your site, add the following: AddHandler application/x-httpd-php .css

根据托管服务提供商的不同,最后一步可能是必要的:您还需要告诉服务器styles.php作为样式表来提供。 在您网站的.htaccess文件中,添加以下内容:AddHandler application / x-httpd-php .css

That's it. We can now go ahead and use any PHP we wish to dynamically generate CSS code in our stylesheet. There are just two downsides:

而已。 现在,我们可以继续使用任何我们希望在样式表中动态生成CSS代码PHP。 有两个缺点:

  1. As a PHP file, the stylesheet must now be generated every time it is called, meaning that the file cannot be cached by the browser.

    作为一个PHP文件,现在必须在每次调用时生成样式表,这意味着浏览器无法缓存该文件。
  2. This generative call results in a small server hit for every visitor.

    这种生成性调用会为每个访问者带来一个小的服务器点击量。

The two effects combine to fractionally slow down page load time: not a significant effect on small, low-traffic websites, but a factor to keep in mind as site traffic increases.

两种影响共同导致页面加载时间减慢:对小型,低流量的网站影响不大,但随着网站流量的增加,要牢记这一因素。

翻译自: https://thenewcode.com/494/Fooling-A-PHP-File-Into-Believing-It-Is-A-Stylesheet

gif文件头欺骗

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值