通过PHP post方法设置CSS属性


Setting CSS property trough PHP post method

我正在尝试动态设置CSS属性。但由于某种原因,使用post方法是行不通的。

这是有效的:

<?php header("Content-type: text/css", 
    "Location:   http://localhost/template/index.php"); 
     $backgcol = '#333';
?>
body
{
  background-color:<?=$backgcol?>;
}

但这不起作用:

<?php header("Content-type: text/css", "Location: http://localhost/template/index.php"); 
     $backgcol = $_POST['color'];
?>
body
{
   background-color:<?=$backgcol?>;
}

这是html:

<!DOCTYPE html>
<html>
   <head>
      <title></title>
      <link rel="stylesheet" type="text/css" href="style.php">
   </head>
   <body>
      <form action="style.php" method="post">
         <input type="text" name="color"><br>
            <input type="submit" value="Set color">
      </form>
   </body>
</html>

您误解了太多要点。

  • 请参阅PHP手册-页眉。第二个参数的用法与您正在考虑的不同。至少应该重复调用:
<?php
header('Content-Type: text/css');
header('Location: http://localhost/template/index.php');
  • Location标头与CSS文件请求无关
  • 不再对CSS文件请求设置$_POST
  • $_POST变量在每个请求中都是临时的。无法保存

有效示例:

setcolor.php

<?php
    const COLOR_REGEX = '/'A#(?:(?:['da-f]{3}){1,2}+|AliceBlue|AntiqueWhite|Aqua|Aquamarine|Azure|Beige|Bisque|Black|BlanchedAlmond|Blue|BlueViolet|Brown|BurlyWood|CadetBlue|Chartreuse|Chocolate|Coral|CornflowerBlue|Cornsilk|Crimson|Cyan|DarkBlue|DarkCyan|DarkGoldenRod|DarkGray|DarkGreen|DarkKhaki|DarkMagenta|DarkOliveGreen|DarkOrange|DarkOrchid|DarkRed|DarkSalmon|DarkSeaGreen|DarkSlateBlue|DarkSlateGray|DarkTurquoise|DarkViolet|DeepPink|DeepSkyBlue|DimGray|DodgerBlue|FireBrick|FloralWhite|ForestGreen|Fuchsia|Gainsboro|GhostWhite|Gold|GoldenRod|Gray|Green|GreenYellow|HoneyDew|HotPink|IndianRed|Indigo|Ivory|Khaki|Lavender|LavenderBlush|LawnGreen|LemonChiffon|LightBlue|LightCoral|LightCyan|LightGoldenRodYellow|LightGray|LightGreen|LightPink|LightSalmon|LightSeaGreen|LightSkyBlue|LightSlateGray|LightSteelBlue|LightYellow|Lime|LimeGreen|Linen|Magenta|Maroon|MediumAquaMarine|MediumBlue|MediumOrchid|MediumPurple|MediumSeaGreen|MediumSlateBlue|MediumSpringGreen|MediumTurquoise|MediumVioletRed|MidnightBlue|MintCream|MistyRose|Moccasin|NavajoWhite|Navy|OldLace|Olive|OliveDrab|Orange|OrangeRed|Orchid|PaleGoldenRod|PaleGreen|PaleTurquoise|PaleVioletRed|PapayaWhip|PeachPuff|Peru|Pink|Plum|PowderBlue|Purple|RebeccaPurple|Red|RosyBrown|RoyalBlue|SaddleBrown|Salmon|SandyBrown|SeaGreen|SeaShell|Sienna|Silver|SkyBlue|SlateBlue|SlateGray|Snow|SpringGreen|SteelBlue|Tan|Teal|Thistle|Tomato|Turquoise|Violet|Wheat|White|WhiteSmoke|Yellow|YellowGreen)'z/i';
    function h($str) {
        return htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); 
    }
    $color = (string)filter_input(INPUT_POST, 'color');
    if ($color !== null) {
        if (preg_match(COLOR_REGEX, $color)) {
            file_put_contents('color.dat', $color, LOCK_EX);
            $msg = 'Color setting has been saved: ' . $color;
        } else {
            $msg = 'Color format is invalid:' . $color;
        }
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link rel="stylesheet" type="text/css" href="style.php">
    </head>
    <body>
<?php if (isset($msg)): ?>
        <p><?=h($msg)?></p>
<?php endif; ?>
        <form action="" method="post">
            <input type="text" name="color"><br>
            <input type="submit" value="Set color">
        </form>
    </body>
</html>

style.php

<?php
header('Content-type: text/css');
$color = is_readable('color.dat') ? file_get_contents('color.dat') : "#ffffff";
?>
body {
    background-color: <?=$color?>;
}