如何在php会话上保存css样式表指针



我想允许用户通过单击按钮来更改我的网站主题颜色。目前,我将 css 指针保存到 url 上。但是当跳转到其他页面时,css指针会从新页面的URL中消失,因此主题颜色恢复为默认值。如何让每个页面记住当前选择的css?我可以将 css 指针保存到 php 会话吗?怎么做?

代码:

<html>
<head>
    <link rel="stylesheet" href="css/theme-<?php if ($css=="blue" || $css == "") echo "blue"; else echo $css; ?>.css" type="text/css" media="screen" title="csstheme" /> 
    <?php require_once("session.php"); ?>       
</head>
    <body>
        <a href="<?php print $_SERVER['PHP_SELF'];?>?<?php print $querystring;?>&css=blue" ><img src="http://plekz.com/images/layouts/blue.jpg" /></a>
        <a href="<?php print $_SERVER['PHP_SELF'];?>?<?php print $querystring;?>&css=green" ><img src="http://plekz.com/images/layouts/green.jpg" /></a> 
        <a href="<?php print $_SERVER['PHP_SELF'];?>?<?php print $querystring;?>&css=pink" ><img src="http://plekz.com/images/layouts/pink.jpg" /></a>
        <a href="<?php print $_SERVER['PHP_SELF'];?>?<?php print $querystring;?>&css=white" ><img src="http://plekz.com/images/layouts/white.jpg" /></a>
        <a href="<?php print $_SERVER['PHP_SELF'];?>?<?php print $querystring;?>&css=red" ><img src="http://plekz.com/images/layouts/red.jpg" /></a>     
    </body>
</html>

是的,会议将是一个很好的地方:

您可以创建一个调用以设置主题会话的文件。例如,通过 QueryString 将所选主题传递给该文件。然后将用户返回到他们来自的页面。

这是未经测试的,但像这样:

if (isset($_GET["theme"]))
{
    session_start();
    $_SESSION["theme"] = $_GET["theme"];
}
header('Location: ' . $_SERVER['HTTP_REFERER']);

您也可以加入 switch-or if 语句,以确保主题会话只能设置为几个预定义的值。

然后,您阅读每个页面上的会话。如果设置了主题变量,则使用该主题,否则回退到默认主题。

为您提供更多信息:

  • 关于会话的 PHP 手册

最新更新