...

View Full Version : Clear all cookies/set cookie



SDP2006
09-19-2003, 10:43 PM
How can I in an if statement

1. clear all cookies previously set
and then
2. set a cookie

BTW, I know how to do #2.....


Thanks

Nightfire
09-20-2003, 03:35 AM
if(isset($_COOKIE['name'])){
setcookie ("name", "", time() - 3600);
}


This will set the expiry time in the past, making the cookie wipe itself out.

http://uk.php.net/manual/en/function.setcookie.php

If you check the manual before you post, you'll get your answer alot faster

SDP2006
09-20-2003, 03:47 AM
Thanks, what I am doing is setting a cookie for the preference of style on my site. What is doing is setting a cookie, and I guess its not overwriting it....here's my script


//This is the form action
<?php
$value = $_POST['colors'];
$blue = "Royale Blue";
$teal = "Teal";
$green = "Hunter Green";
$maroon = "Maroon";
$grey = "Grey";

if ($value == $blue){
setcookie("blue",time()+3600);}

elseif ($value == $teal){
setcookie("teal",time()+3600);}

elseif ($value == $green){
setcookie("green",time()+3600);}

elseif ($value == $maroon){
setcookie("maroon",time()+3600);}

elseif ($value == $grey){
setcookie("grey",time()+3600);}

else
{
echo "Nothing isset!";
}

?>

and heres what checks


//Checks for cookie, then echos a style sheet
<?php
if (isset($_COOKIE['blue'])){
echo "<link rel=stylesheet href=styleblue.css type=text/css>";}

elseif (isset($_COOKIE['teal'])){
echo "<link rel=stylesheet href=styleteal.css type=text/css>";}

elseif (isset($_COOKIE['green'])){
echo "<link rel=stylesheet href=stylegreen.css type=text/css>";}

elseif (isset($_COOKIE['maroon'])){
echo "<link rel=stylesheet href=stylemaroon.css type=text/css>";}

elseif (isset($_COOKIE['grey'])){
echo "<link rel=stylesheet href=stylegrey.css type=text/css>";}

else {echo "<link rel=tylesheet href=style.css type=text/css>";}
?>It will work the first or couple of times you select a different color, but then after the first or second time, it wont do it until the cookie times out.....
If you need an example of what I am trying to accomplish, visit http://www.net-riches.com/includes/categories.php?cat=home and scroll all the way to the bottom

Thanks

SDP2006
09-20-2003, 02:02 PM
Help anyone?:thumbsup:

SDP2006
09-20-2003, 09:27 PM
Anybody?

Nightfire
09-20-2003, 09:33 PM
Why not save yourself time and only use one cookie instead of 5 or so? Just change the value in the cookie to the colour you want. If the cookie's set, echo the cookie's value into the stylesheet link, if it's not set, load the default style

SDP2006
09-20-2003, 09:38 PM
Could you show me an example, Nightfire?

Thanks a million...you guys are great

Nightfire
09-20-2003, 09:45 PM
// set cookie from a link page.php?styles=blue
$value = $_GET['styles'];

setcookie("stylesheet",$value,time()+3600);

// check for cookie and show correct stylesheet

if(isset($_COOKIE['stylesheet'])){
$link = $_COOKIE['stylesheet'].'css';
}else{
$link = "defaultstyle.css";
}

echo '<link rel="stylesheet" type="text/css" href="'.$link.'" />';

SDP2006
09-20-2003, 09:46 PM
I've seen something before like

if (isset($_COOKIE['name'])){
echo "<link rel=stylesheet href=$style>";

not using the $_GET['method'];

and $style was set in the cookie....how can I do that?

SDP2006
09-20-2003, 10:04 PM
Okay, for some reason it is echoing stylecss instead of style.css....
Here is the code


<?php
if(isset($_COOKIE['stylesheet'])){
$link = $_COOKIE['stylesheet'].'.css';
}else{
$link = "style.css";
}
echo '<link rel=stylesheet type=text/css href='.$link'>';
?>

Nightfire
09-21-2003, 12:23 AM
echo '<link rel=stylesheet type=text/css href='.$link.'>';

You're missing a . after $link

SDP2006
09-21-2003, 02:43 AM
Works wonderful! Thanks so much guys!!

SDP2006
09-22-2003, 10:38 PM
Final product -

http://www.net-riches.com/includes/categories.php?cat=home

Nightfire
09-23-2003, 01:07 AM
Glad to have helped :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum