gpt4 book ai didi

php - 如何将cookie更改为 session ?

转载 作者:行者123 更新时间:2023-12-04 05:17:55 25 4
gpt4 key购买 nike

我是 session 和 cookie 的新手。就像我想将 cookie 更改为 session 的磁贴。我只是从这里的链接获取代码:http://www.pixelconnect.com.au/web-design-blog/php-css-theme-switcher-with-cookies ?
我尝试了一些方法,但没有奏效。

在文档类型之前:

<?php
$theme1 = business;
$theme2 = modern;
$theme3 = web2;
if(isset($_POST['style']))
{setcookie('style', $_POST['style'], time()+(60*60*24*1000));
$style=$_POST['style'];}
elseif(isset($_COOKIE['style']))
{$style=$_COOKIE['style'];}
else
{$style=$theme1;} ?>

头:
<head>
<link href="<?PHP echo $style; ?>.css" rel="stylesheet" type="text/css" />
</head>

body :
<body>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<select name="style"> <option <?php echo "value='$theme1'";
if($style == $theme1)
{
echo "selected='selected'";
}
?>><?php echo $theme1; ?></option>
<option <?php
echo "value='$theme2'";
if($style == $theme2)
{
echo "selected='selected'";
}
?>><?php echo $theme2; ?></option>
<option <?php
echo "value='$theme3'";
if($style == $theme3)
{
echo "selected='selected'";
}
?>><?php echo $theme3; ?></option>
</select><input type="submit" />
</form>
</body>

我想做类似的事情:
session_start();
$theme1
$theme2
$theme3
if(isset($_POST['style'])){
$style=$_POST['style'];}
elseif(isset($_SESSION['style']))
{$style=$_SESSION['style'];}
else
{$style=$theme1;}
?>

头:
<link rel="stylesheet" type="text/css" href="<?php echo $_SESSION['style']?>" />

最佳答案

如果您想使用 session 而不是 cookie,请将您的打开代码更改为此(未经测试):

<?php
session_start();
$theme1 = business;
$theme2 = modern;
$theme3 = web2;
if (isset($_POST['style'])) {
$_SESSION['style'] = $_POST['style'];
$style=$_POST['style'];
} else if (isset($_SESSION['style'])) {
$style=$_SESSION['style'];
} else {
$style=$theme1;
} ?>

关于php - 如何将cookie更改为 session ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14000264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com