2

私はセッションとクッキーが初めてです。タイルのように、Cookie をセッションに変更したい。ここのリンクからコードを取得します: http://www.pixelconnect.com.au/web-design-blog/php-css-theme-switcher-with-cookies ? いくつかの方法を試しましたが、うまくいきませんでした。

Doctypeの前:

<?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>
<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']?>" />
4

3 に答える 3

1

セッションでクッキーを破棄したい場合は、時間パラメータを空白のままにしてください。

    setcookie('style', $_POST['style']);

十分でしょう...

于 2012-12-22T05:18:06.223 に答える
1

とてもシンプルです。PHPから、呼び出すだけですsetcookie

PHP セッションの Cookieを無効にしたい場合は、ini_set("session.use_cookies",0);ini_set("session.use_trans_sid",1);

于 2012-12-22T05:18:23.547 に答える
1

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;
} ?>
于 2012-12-22T05:21:17.137 に答える