简体   繁体   English

PHP样式表不起作用?

[英]PHP Stylesheet won't work?

Okay, so I've done a bit of research into using PHP to allow using variables in CSS. 好的,所以我对使用PHP允许在CSS中使用变量进行了一些研究。 I know it'll have a hit on the performance, but that doesn't bother, what bothers me is that I can't get the PHP stylesheet to work as it should. 我知道它会影响性能,但这并不会打扰,令我困扰的是我无法使PHP样式表正常工作。

I'm linking the stylesheet with the following code 我将样式表与以下代码链接

<link rel="stylesheet" type="text/css" media="screen" href="/res/css/default.php">

And in the PHP stylesheet I'm using the header to read the text as CSS: 在PHP样式表中,我使用标题将文本读取为CSS:

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

However I'm not getting the results I want at all, you can see the the problem by going to my website, www.andysinventions.info. 但是我根本没有得到想要的结果,您可以通过转到我的网站www.andysinventions.info看到问题。

Checking the site in Google Chrome with the console open displays this error too, 在控制台打开的情况下检查Google Chrome浏览器中的网站,也会显示此错误,

"Resource interpreted as Stylesheet but transferred with MIME type text/plain." “资源被解释为样式表,但以MIME类型的text / plain传输。” If anyone could explain to me what this meant it might help me resolve the issue. 如果有人可以向我解释这意味着什么,则可以帮助我解决问题。

If there's anything else you need, let me know and I'll fetch it for you if I can. 如果您还有其他需要,请告诉我,如果可以,我会为您取货。

Thank you, Andy. 谢谢你,安迪

- --

Got to go for now, however I will be back later to check answers/solutions. 现在可以走了,但是我稍后会回来检查答案/解决方案。

When I go to http://www.andysinventions.info/res/css/default.php I get to see the PHP code. 当我转到http://www.andysinventions.info/res/css/default.php时,我会看到PHP代码。 That means it's not getting parsed. 这意味着它没有被解析。

您必须将Web服务器配置为将以.css结尾的文件解释为PHP,或者将文件重命名为/res/css/default.php

When you open the stylesheet in your browser, you get to see the PHP code: 在浏览器中打开样式表时,您将看到PHP代码:

<?php header("Content-type: text/css"); ?>
<?php
$primary = 'orange';
$secondary = 'rgb(55,55,55)';
$trim = 'white';
?>
/*Main*/
*, html{
margin:0;
padding:0;
font-family: arial;
}

Somehow the stylesheet is not interpret as PHP code. 样式表无法以某种方式解释为PHP代码。 Maybe you have an htaccess file that prevents this? 也许您有一个防止这种情况的htaccess文件?

Its getting read in at runtime as a css file not a php file so your server is never processing the php. 它在运行时作为css文件而不是php文件被读取,因此您的服务器从不处理php。 That link line is getting read by the client's browser and the client machine is making another request for what it thinks is a flat css file and that's what its getting whether you have php in it or not. 客户端浏览器读取了该链接行,并且客户端计算机再次请求它认为是平面css文件,无论是否包含php,这都是它所得到的。

I've never done this but I would think you'd have to use an include or a require to get closer to the results that you're looking for. 我从未做过此事,但我认为您必须使用包含或要求才能更接近所需的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM