0

所以我在我的网站上有一些 CSS3 的特殊效果,但我试图给观众一个禁用这些的选项。所以我将使用 PHP Cookie 方法,但它根本不起作用。

如果没有任何 cookie,即使它们已经启用,我也会看到“单击此处启用特殊效果”。直接进入禁用页面也不会关闭它们。

这是我的代码中的一些片段。

检查是否设置了 cookie:

<?php
if(!$_COOKIE['nofancy']){ 
?>
<link rel="stylesheet" type="text/css" href="http://pattersoncode.ca/incls/nofancy.css" media="screen" />
<?php
}
?>

关闭特殊效果:

<p>You have successfully turned off all special effects used in this website. This effect will last 48 hours after which you will need to disable them again. You will be redirected to the homepage in 5 seconds.</p>
<?php
$value = "nofancy";
setcookie("nofancy",$value, time()+3600*48);
?>
<script type='text/javascript'>
setTimeout(function () {
   window.location.href= 'http://www.pattersoncode.ca';

},5000); // 5 seconds
</script>

重新打开特殊效果:

<p>Special effects have been enabled again. This effect will be permanent unless disabled again. You will be redirected to the homepage in 5 seconds.</p>
<?php
setcookie ("nofancy", "", time() - 3600);
?>
<script type='text/javascript'>
setTimeout(function () {
   window.location.href= 'http://www.pattersoncode.ca'; 

},5000); // 5 seconds
</script>

禁用/启用按钮:

<?php
if (!isset($_COOKIE["nofancy"]))
{
?>
 <a href="?a=fancyon"> Turn special effects back on </a> 
<?php
} else {
?>
 <a href="?a=nofancy"> Remove all effects </a> 
<?php
}
?>

任何人都可以帮助我?好难过。。

4

1 回答 1

1

我建议您设置 CSS,以便所有启用花哨的东西的选择器都以这样的附加类开头:

/* regular CSS - just to demonstrate the normal selectors that have
   nothing to do with fancy CSS stuff */
#foo {
    border: 1px solid hotpink;
    width: 300px;
    height: 200px;
}

/* fancy stuff added here */
.fancy-css #foo {
    box-shadow: 5px 5px 10px lime;
    border-radius: 4px;
}

这样,您可以通过控制 HTML 输出来简单地启用/禁用样式。风格活跃?将班级放在body标签上。样式不活跃?不要把它放在那里。

这样,您可以通过相应地修改 HTML 输出来控制服务器上的样式。您还可以通过简单地将类删除/添加到body标签来切换花哨的东西而无需重新加载。

于 2013-07-08T21:59:49.370 回答