通过PHP post方法设置CSS属性

时间:2015-02-13 09:07:32

标签: php css post

我正在尝试动态设置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>

1 个答案:

答案 0 :(得分:0)

你对太多的观点有误解。

  • 请参阅 PHP Manual - header 。第二个参数用法与您考虑的不同。至少应该重复调用它:
<?php
header('Content-Type: text/css');
header('Location: http://localhost/template/index.php');
  • Location标头与CSS文件请求无关。
  • $_POST值不再设置在CSS文件请求中。
  • $_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?>;
}