Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何创建具有两种样式的web_Php_Html_Css - Fatal编程技术网

Php 如何创建具有两种样式的web

Php 如何创建具有两种样式的web,php,html,css,Php,Html,Css,我希望我的网站的用户能够选择他们想要的风格,手写或基本。 为此,我创建了两个样式表和一个选择框,这样当您选择一个选项时,样式链接将被收费。由于该网站有许多页面,我正在考虑使用$\u SESSION值,但默认情况下如何设置?有更好的选择吗 谢谢你的回答 假设您想将所选样式表存储在$\u会话['style']中,那么您可以设置默认值: if( empty( $_SESSION['style'] )) { // Set $_SESSION['style'] to what you want a

我希望我的网站的用户能够选择他们想要的风格,手写或基本。 为此,我创建了两个样式表和一个选择框,这样当您选择一个选项时,样式链接将被收费。由于该网站有许多页面,我正在考虑使用$\u SESSION值,但默认情况下如何设置?有更好的选择吗


谢谢你的回答

假设您想将所选样式表存储在
$\u会话['style']
中,那么您可以设置默认值:

if( empty( $_SESSION['style'] )) {
    // Set $_SESSION['style'] to what you want as the default
}
您可以尝试:

$style = 'default';
if(!empty($_SESSION['style'])) {
  $style = $_SESSION['style'];
}

我认为更好的选择是搭配饼干。无论是cookies还是会话,您仍然需要检查该值是否被定义,如果不是,定义它。

您可能需要考虑将用户的选择存储在Cookie中。这很容易使用JavaScript。您只需读取和写入
document.cookie即可。另请参阅关于阅读cookie的答案

类似于

function storeStyle(style) {
    var cookieSubStrings = [], cookies = readCookies();
    cookies.style = style;
    if (!("expires" in cookies))
        cookies.expires = (new Date().getTime() + 30*24*60*60*1000).toGMTString();
    for (e in cookies)
        if (cookies.hasOwnProperty(e))
            cookieSubStrings.push(e + "=" + cookies[e]);
    document.cookie = cookieSubStrings.join("; ")
}

var style, cookies;
cookies = readCookies();
if (!("style" in cookies)) {
    style = "basic";
    storeStyle(style);
} else {
    style = cookies.style;
}
这就足够了。然后可以使用
style
变量加载相应的样式表