surisava
surisava

Reputation: 155

how to change cookie to session?

I'm new to session and cookie. Like the tile I want to change cookie to session. I just get code from the link here: http://www.pixelconnect.com.au/web-design-blog/php-css-theme-switcher-with-cookies? I've tried some way but it didn't work.

before 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:

<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>

I'd like to do something like:

session_start();
$theme1
$theme2
$theme3
if(isset($_POST['style'])){
$style=$_POST['style'];}
elseif(isset($_SESSION['style']))
{$style=$_SESSION['style'];}
else
{$style=$theme1;} 
?>

head:

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

Upvotes: 2

Views: 2895

Answers (3)

Gareth Cornish
Gareth Cornish

Reputation: 4356

If you want to use the session instead of cookies, change your opening code to this (untested):

<?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;
} ?>

Upvotes: 1

paulsm4
paulsm4

Reputation: 121881

It's really very simple. From PHP, just call setcookie

If you want to disable cookies for PHP sessions, just call ini_set("session.use_cookies",0);ini_set("session.use_trans_sid",1);

Upvotes: 1

William Buttlicker
William Buttlicker

Reputation: 6000

If you want to destroy cookiee with session , just leave the time parameter blank i.e

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

would be enough...

Upvotes: 1

Related Questions