我试着在PHP CSS文件中加入一个变化的背景,但即使我尝试,我的网页也是空白的,或者背景是空的……
服务器基于Linux,运行PHP7和Apache2。
index.php (版本1)
<link rel="stylesheet" type="text/css" href="./css/background.php.css">index.php (版本2)
<body>
<style>
<?php
include("./css/background.php.css");
?>
</style>
</body>background.php.css
<?php
echo("
@charset "utf-8";
/* CSS Document */
");
error_reporting(E_ALL);
$choicer = rand(1, 9);
if ($choicer == 1) {
echo('
body{
background-color: #333;
background-image: url("../imgs/bg$choicer.jpg");
background-attachment: fixed;
background-position: center;
background-size: 100% 100%;
background-repeat: no-repeat;
}
');
}
else {
if ($choicer == 2) {
echo('
body{
background-color: #333;
background-image: url("../imgs/bg$choicer.jpg");
background-attachment: fixed;
background-position: center;
background-size: 100% 100%;
background-repeat: no-repeat;
}
');
}
else {
if ($choicer == 3) {
echo('
body{
background-color: #333;
background-image: url("../imgs/bg$choicer.jpg");
background-attachment: fixed;
background-position: center;
background-size: 100% 100%;
background-repeat: no-repeat;
}
');
}
else {
if ($choicer == 4) {
echo('
body{
background-color: #333;
background-image: url("../imgs/bg$choicer.jpg");
background-attachment: fixed;
background-position: center;
background-size: 100% 100%;
background-repeat: no-repeat;
}
');
}
else {
if ($choicer == 5) {
echo('
body{
background-color: #333;
background-image: url("../imgs/bg$choicer.jpg");
background-attachment: fixed;
background-position: center;
background-size: 100% 100%;
background-repeat: no-repeat;
}
');
}
else {
if ($choicer == 6) {
echo('
body{
background-color: #333;
background-image: url("../imgs/bg$choicer.jpg");
background-attachment: fixed;
background-position: center;
background-size: 100% 100%;
background-repeat: no-repeat;
}
');
}
else {
if ($choicer == 7) {
echo('
body{
background-color: #333;
background-image: url("../imgs/bg$choicer.jpg");
background-attachment: fixed;
background-position: center;
background-size: 100% 100%;
background-repeat: no-repeat;
}
');
}
else {
if ($choicer == 8) {
echo('
body{
background-color: #333;
background-image: url("../imgs/bg$choicer.jpg");
background-attachment: fixed;
background-position: center;
background-size: 100% 100%;
background-repeat: no-repeat;
}
');
}
else {
echo('
body{
background-color: #333;
background-image: url("../imgs/bg$choicer.jpg");
background-attachment: fixed;
background-position: center;
background-size: 100% 100%;
background-repeat: no-repeat;
}
');
}
}
}
}
}
}
}
}
?>我希望有人能帮我解决这个问题。
发布于 2019-06-12 10:07:25
这是可以做到的。我已经做过很多次了,它不需要在web服务器的级别上做任何改变。人们常犯的错误是认为在HTML页面中加载CSS文件时文件扩展名很重要。
标记已经告诉浏览器将文件加载为CSS。现在,如果浏览器试图聪明地检查文件的MIME类型,那么您就可以在PHP文件中设置正确的头文件。我已经这样做了几次,并取得了成功。这是一个我刚刚在Chrome,Firefox和Safari上测试过的例子。
<?php
// css.php
header("Content-type: text/css; charset: UTF-8");
echo <<<CSS
p {
color: red;
}
body {
font-size: 3em;
}
CSS;在本例中,我使用heredoc语法,以避免在需要时过多地使用转义引号。此外,这样代码的可读性更好。
注意,我正在设置文件的内容类型和字符集。此技术可用于使PHP生成图像(jpeg、png等)。或者PDF、Excel、Word文档。您的想象力和对需要生成的文档格式的了解是极限。
加载这个PHP生成的CSS文件的HTML文件可能类似于以下代码...
<!-- index.html -->
<html>
<head>
<title>CSS + PHP</title>
<link rel="stylesheet" href="css.php">
<body>
<p>This is a paragraph</p>
</body>
</head>
</html>更好的是,你可以把你的CSS放在一个单独的文件中。例如,style.css并包含css.php文件中的该文件。
<?php
// css.php
header("Content-type: text/css; charset: UTF-8");
print file_get_contents("style.css");https://stackoverflow.com/questions/56548571
复制相似问题