【问题标题】:Convert js to php for background color cookie将 js 转换为 php 以获得背景颜色 cookie
【发布时间】:2019-07-12 04:37:08
【问题描述】:

我正在使用以下 js 脚本为用户更改的背景颜色添加 cookie,但是加载颜色需要大约 1 秒的时间,我认为使用 php 作为 cookie 会更好写从加载页面时选择的颜色,因此从一开始就以正确的颜色加载。

JS

var setCookie = function (n, val) {
    var exdays = 30;
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = n + "=" + val + "; " + expires;
};

var getCookie = function (n) {
    var name = n + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1);
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
};

document.onclick = function (e) {
    if (e.target.className == 'color-btn') {
        var favColor = e.target.style.backgroundColor;
        setCookie('color', favColor);
        document.body.style.backgroundColor = favColor;
        console.log(favColor);
    }
};

window.onload = function () {
    var favColor = document.body.style.backgroundColor;
    var color = getCookie('color');
    if (color === '') {
        document.body.style.backgroundColor = favColor;
    } else {
      document.body.style.backgroundColor = color;
    }
};
      body {
  background-color: rgba(219, 218, 236, 1);
}

.color-btn {
  width: 100px;
  height: 25px;
  display: inline-block;
  cursor: pointer;
  margin: 0;
  padding: 0;
  border: 1px solid white;
}
<li><button class="color-btn" style="background-color: #dbdaec"></button></li>
<li><button class="color-btn" style="background-color: #B4CAE5"></button></li>
<li><button class="color-btn" style="background-color: #C2E5C6"></button></li>
<li><button class="color-btn" style="background-color: #EAEAEA"></button></li>
<li><button class="color-btn" style="background-color: #ffffff"></button></li>

注意:以上脚本在沙箱中,在这里不起作用。

转换后的php(即不起作用):

<?php
$setCookie = new Func(function($n = null, $val = null) use (&$Date, &$document) {
  $exdays = 30.0;
  $d = _new($Date);
  call_method($d, "setTime", _plus(call_method($d, "getTime"), to_number($exdays) * 24.0 * 60.0 * 60.0 * 1000.0));
  $expires = _concat("expires=", call_method($d, "toGMTString"));
  set($document, "cookie", _concat($n, "=", $val, "; ", $expires));
});
$getCookie = new Func(function($n = null) use (&$document) {
  $name = _concat($n, "=");
  $ca = call_method(get($document, "cookie"), "split", ";");
  for ($i = 0.0; $i < get($ca, "length"); $i++) {
    $c = get($ca, $i);
    while (eq(call_method($c, "charAt", 0.0), " ")) {
      $c = call_method($c, "substring", 1.0);
    }
    if (eq(call_method($c, "indexOf", $name), 0.0)) {
      return call_method($c, "substring", get($name, "length"), get($c, "length"));
    }
  }
  return "";
});
set($document, "onclick", new Func(function($e = null) use (&$setCookie, &$document, &$console) {
  if (eq(get(get($e, "target"), "className"), "color-btn")) {
    $favColor = get(get(get($e, "target"), "style"), "backgroundColor");
    call($setCookie, "color", $favColor);
    set(get(get($document, "body"), "style"), "backgroundColor", $favColor);
    call_method($console, "log", $favColor);
  }
}));
set($window, "onload", new Func(function() use (&$document, &$getCookie) {
  $favColor = get(get(get($document, "body"), "style"), "backgroundColor");
  $color = call($getCookie, "color");
  if ($color === "") {
    set(get(get($document, "body"), "style"), "backgroundColor", $favColor);
  } else {
    set(get(get($document, "body"), "style"), "backgroundColor", $color);
  }

}));
?>

【问题讨论】:

  • 您不必用 PHP 编写 cookie。您可以保留 JS 代码原样,并在下次加载页面时使用 PHP 读取该值,并根据该值在服务器端设置背景颜色。话虽这么说,我可以用 PHP 做,但我的知识有限,你的代码在我看来像中文:) (...而且我不会说中文,所以...哈哈)

标签: javascript php jquery html css


【解决方案1】:

在 PHP 中设置 cookie:

setcookie("bgcolor", $value, time()+3600);

在 PHP 中获取 cookie 并运行快速验证以确保其中没有任何不应该存在的内容。

$bgcolor = $_COOKIE["bgcolor"];
if(ctype_xdigit(substr($bgcolor ,1)) == false || strlen(ltrim($bgcolor ,"#"))!=6){
   $bgcolor = "#ffffff";
}

将该变量输出到屏幕:

<?=bgcolor?>

【讨论】:

  • 感谢您的回复,您可以根据您的建议编辑上面的代码吗?我不确定在哪里编辑这些行..
【解决方案2】:

这是一个有效的简化版本:

<?php
$colors = ['#dbdaec', '#B4CAE5', '#C2E5C6', '#EAEAEA', '#ffffff'];
// Default color
$color = $colors[4];
// Try to read the cookie
if(isset($_COOKIE["color"])) {
  // If it exists, set the color to its value
  $color = $_COOKIE["color"];
}
?><!DOCTYPE html>
<html>
<head>
  <style>
  body { background-color: <?=$color?>; } /* Here, use the color */
  .color-btn {
    width: 100px; height: 25px;
    display: inline-block;
    cursor: pointer;
    margin: 0; padding: 0;
    border: 1px solid white;
  }
  </style>
</head>
<body>
  <ul><?php foreach ($colors as $c) { ?>
      <li><button class="color-btn" style="background-color: <?=$c?>"></button></li>
    <?php } ?>
  </ul>
  <script>
  // I did not edit your JS other than remove unused blocks
  var setCookie = function (n, val) {
    var exdays = 30;
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = n + "=" + val + "; " + expires;
  };
  document.onclick = function (e) {
    if (e.target.className == 'color-btn') {
      var favColor = e.target.style.backgroundColor;
      setCookie('color', favColor);
      document.body.style.backgroundColor = favColor;
      console.log(favColor);
    }
  };
  </script>
</body>
</html>

它使用JS设置cookie客户端,使页面不会重新加载。并且下次您重新加载页面或转到另一个页面时,PHP 将读取该 cookie 并在服务器端设置该背景颜色,以便没有颜色闪烁。

【讨论】:

  • 谢谢,我如何设置添加选项供用户选择颜色,与您在此处的回答相同:stackoverflow.com/a/53904771/1114465 with php and expiry date?你能编辑这个答案吗?
  • 太好了.. 但是为什么在选择颜色时它会重新加载页面?我猜想使用 jquery 的另一个示例会更改单击时的颜色.. 或者在这种情况下无法完成?
  • 您想在 PHP 中设置 cookie。由于 PHP 在后端执行,您要么需要重新加载页面,要么使用 Ajax 发送颜色。我将使用 Ajax 创建一个示例。
  • 实际上,不,我不会创建使用 Ajax 的示例。我在这里发布的第一个答案是正确的。它在 JS 中设置 cookie(因此,无需重新加载页面),然后当您重新加载页面时,使用 PHP 读取 cookie。我会把那个答案放回去
  • 我尝试在小提琴中再次测试,它确实重新加载了页面:main.xfiddle.com/code_50964467.php 我尝试删除type="submit",但同样的问题,我认为是因为action="" method="POST"
猜你喜欢
  • 2019-09-29
  • 2015-11-07
  • 2011-11-19
  • 1970-01-01
  • 2020-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-11
相关资源
最近更新 更多