Jump to bottom

Closed Thread Icon

Topic awaiting preservation: php styleswitcher... (Page 1 of 2) Pages that link to <a href="http://ozoneasylum.com/backlink?for=12021" title="Pages that link to Topic awaiting preservation: php styleswitcher... (Page 1 of 2)" rel="nofollow" >Topic awaiting preservation: php styleswitcher... <span class="small">(Page 1 of 2)</span>\

 
YoBoyE
Nervous Wreck (II) Inmate

From:
Insane since: Sep 2001

posted posted 01-28-2002 00:45

i made a php styleswitcher...but with one drawback. I can't figure out how to set cookies to where the theme the user selects is the one that is active when they visit my site. http://66.34.243.107/styleswitcher.php .
the code for it is...

code:
<html>

<head>

<?php

if ($theme == blue)

{

$style="<link rel=\"stylesheet\" type=\"text/css\" href=\"blue.css\" />";

}

elseif ($theme == red)

{

$style="<link rel=\"stylesheet\" type=\"text/css\" href=\"red.css\" />";

}

else

{

$style="<link rel=\"stylesheet\" type=\"text/css\" href=\"default.css\" />";

}

echo "$style";

?>

</head>

<body>

does this work?<br /><br />

<a href="styleswitcher.php?theme=blue">blue stylesheet</a><br /><br />
<a href="styleswitcher.php?theme=red">red stylesheet</a><br /><br />
<a href="styleswitcher.php?theme=default">default stylesheet</a><br /><br />

</body>

</html>



i was just wondering if someone could please help me figure this out. i'm pretty stuck. thanks in advance.

[edit: fixed the code]

Eric Hesterman

[This message has been edited by YoBoyE (edited 01-28-2002).]

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 01-28-2002 07:02

Well, you didn't mention whether you are using PHP or JavaScript to set/read cookies. PHP info would be at http://www.php.net/manual/en/function.setcookie.php

I haven't done much with cookies, yet. I should find some good tutorials.

YoBoyE
Nervous Wreck (II) Inmate

From:
Insane since: Sep 2001

posted posted 01-28-2002 23:40

i'm trying to set the cookies with php. i'll post the code i'm using that generates errors later. i have a school function tonight.

Eric Hesterman

Sash
Paranoid (IV) Inmate

From: Canada, Toronto
Insane since: May 2000

posted posted 01-29-2002 02:57

I am not a PHP guy (yet) but I played a bit here:
http://www.cs.yorku.ca/~cs221077

Basically I have change.php that has only this:

<?
setcookie ("CSSPref", $skin, time()+604800);

Header ("Location: $HTTP_REFERER")
?>

On each page I have this:

$skin = $HTTP_COOKIE_VARS["CSSPref"];
if ($skin == "") {
$CSSPref = "daisy";
}

daisy is the default skin.

Of course this is very simple, I know. There should be done more, like what happens if the referrer is from outside...
But this is basic, you can play around with it later.

sasha »

[This message has been edited by Sash (edited 01-29-2002).]

YoBoyE
Nervous Wreck (II) Inmate

From:
Insane since: Sep 2001

posted posted 01-29-2002 03:23

thanks. it makes sense, except for the fact I'm not sure where i specify, for examle, what stylesheet 'daisy' will link to. do I assign a variable to daisy or something? thanks!

Eric Hesterman

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 01-29-2002 03:31

What I would do is use a switch function to point to the correct .css file for the desired theme.

Such as:

switch ($theme){
case blue:
print("<link rel=\"stylesheet\" type=\"text/css\" href=\"blue.css\" />\n");
break;
case green:
print("<link rel=\"stylesheet\" type=\"text/css\" href=\"green.css\" />\n");
break;
case red:
print("<link rel=\"stylesheet\" type=\"text/css\" href=\"red.css\" />\n");
break;
default:
print("<link rel=\"stylesheet\" type=\"text/css\" href=\"default.css\" />\n");
}



[This message has been edited by Pugzly (edited 01-29-2002).]

Sash
Paranoid (IV) Inmate

From: Canada, Toronto
Insane since: May 2000

posted posted 01-29-2002 03:32

Well, I called it "daisy" because I have flower skins over there, but you can call it whatever you like.
Then you do something like this:

<?
if ($skin == "poppy") {
echo ("<style type=\"text/css\" media=\"screen\">@import \"css/poppy.css\";</style>\n");
} elseif ($skin == "sunflower") {
echo ("<style type=\"text/css\" media=\"screen\">@import \"css/sunflower.css\";</style>\n");
} elseif ($skin == "violet") {
echo ("<style type=\"text/css\" media=\"screen\">@import \"css/violet.css\";</style>\n");
} elseif ($skin == "daisy") {
echo ("<style type=\"text/css\" media=\"screen\">@import \"css/daisy.css\";</style>\n");
} else {
echo ("<style type=\"text/css\" media=\"screen\">@import \"css/daisy.css\";</style>\n");
}
?>

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 01-29-2002 03:43

Sash - looks like we posted around the same time..

I like your method, using the HEADER. However, the SWITCH function is probably a little cleaner, since you don't have all of those IF statements.

I'm gonna save your little bit of code and keep it for an idea....

Sash
Paranoid (IV) Inmate

From: Canada, Toronto
Insane since: May 2000

posted posted 01-29-2002 03:46

By all means, Pugzly. I am still digging through PHP so bear with me :-)

However on my homepage (ASP though) I am using switch which is select case in ASP. And yes it is much cleaner.


sasha »

[This message has been edited by Sash (edited 01-29-2002).]

YoBoyE
Nervous Wreck (II) Inmate

From:
Insane since: Sep 2001

posted posted 01-29-2002 04:07

thank you so much for helping me! it's working just how i want it to. now that i have a basis of understanding for it, i may take a stab at playing with it in my free time. you can check it out at http://66.34.243.107/styletest.php . i haven't applied it to my actual site yet, that is just a test page. thannks again!!

Eric Hesterman

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 01-29-2002 04:34

Sash - care to post the code you use for first writing the cookie (if there isn't one)? I'd be interested in seeing how you do it. Or do you not write one till they change the skin?



[This message has been edited by Pugzly (edited 01-29-2002).]

Sash
Paranoid (IV) Inmate

From: Canada, Toronto
Insane since: May 2000

posted posted 01-29-2002 05:25

Yes I am setting the cookie only when another skin is selected, if you are talking about the page above (flower skins, PHP).
And the code above is basically it.

Referrer thing can go wrong if the URL is posted here, like http://www.yourpage.com/change.php?skin=something it would go back here. I didn't bother doing it for the page above, but you can validate it and send back to your index page if the referring URL is not yours.

I did that on my homepage using ASP. http://www.sashadesign.com/change.asp?MyMood=photoshop

u-neek
Bipolar (III) Inmate

From: Berlin, Germany
Insane since: Jan 2001

posted posted 01-29-2002 13:49

When you want to change the layout and save the current setting in a cookie use something like:

<?php
$layout = $_GET["layout"];
$expire = time() + (60*60*24*365);
setcookie("Cookie", $layout, $expire);
$where = getenv("HTTP_REFERER");
header ("Location: $where");
exit;
?>

And put a link to your homepage like this: www.mydomain.com/change.php?layout=urban

Put that code into your header:
if (isset($HTTP_COOKIE_VARS["Cookie"])) $layout = $HTTP_COOKIE_VARS["Cookie"]; else $layout = "default";

And use this method to import your stylesheet:
<style type="text/css" media="screen">@import "<?php echo $layout; ?>.css";
Or:
<link rel="stylesheet" type="text/css" href="<?php echo $layout; ?>.css";/>

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 01-29-2002 17:08

Hmm... Can you access cookies set with JS via PHP?



:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 01-30-2002 07:00

Yes, you can access cookies set by JavaScript from PHP, but you must ensure that they are set with same params (i.e. date, path, etc.) and page must be reloaded, of course...


Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 01-30-2002 20:22

With PHP cookies, am I correct to assume that if I want to store more than one variable I should us multiple cookies?

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 01-30-2002 21:14

Not necessarily. There is a limited number of cookies that a browser will accept from a site.
It's most common to stick one parent string in a the cookie that can be parsed (I love explode) into individual variables.



:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 01-30-2002 23:19

<runs off and plays with explode(), then returns>

Hey - that's pretty cool! do you typically just store the values, or do you store the variables with the values and further carve up the string?

Lurch
Paranoid (IV) Inmate

From: Behind the Wheel
Insane since: Jan 2002

posted posted 02-27-2002 09:06

you could also set the cookie to have an id that is then used in a mysql query, drawing more values from a database... this is from a user login script but basically the same thing...

code:
/*

create table cookies (
id bigint auto_increment primary key,
auth bigint not null,
last_used datetime not null,
user_id bigint
);

*/

$cookie_ok = 0;
$user_id = 0;

$cookie_id += 0;
$cookie_auth += 0;

if ($cookie_id > 0) {
// if the browser sent us a cookie, and its special magic
// authorization number matches what's in the database, then we're
// done.

$q = mysql_query ("SELECT user_id FROM cookies WHERE
auth = $cookie_auth AND
id = $cookie_id");
if ($q && ($row = mysql_fetch_object ($q))) {
$cookie_ok = 1;
$user_id = $row->user_id;
mysql_query ("UPDATE cookies SET last_used=now() WHERE
id = $cookie_id");
}
}
if (! $cookie_ok) {
// otherwise, make a new cookie, and send that to the browser.

$cookie_auth = 1 + round (rand ());
mysql_query ("INSERT INTO cookies (auth, last_used)
VALUES ($cookie_auth, now())");
$q = mysql_query ("SELECT last_insert_id() as id");
$row = mysql_fetch_object ($q);
$cookie_id = $row->id;
setcookie ("cookie_id", $cookie_id);
setcookie ("cookie_auth", $cookie_auth);
}

// test this by showing the cookie ID & auth number in the browser's
// title bar

echo "cookie_id is $cookie_id ... cookie_auth is $cookie_auth ... user_id is $user_id";
echo "<P>";



--Lurch--

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 03-05-2002 15:37

Is it just me, or does this style thing NOT add anything to the history (forward and back buttons)?
My example: http://development.runningwolf.com/code/php/themes/index2.php

I'm actually using two cookies - one for the colors, and one for the language. I'd be interested in seeing code example of doing this with explode and a single cookie. When changing the value of either the color OR the language, do I need to explode the current value, change the one value, and reassemble the string to write it back to the cookie?

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 03-07-2002 17:44

bump

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 03-08-2002 00:35

hokay it's actually 2 explodes say you set a cookie called style and it looks like this, you can use virtually any seperator but we know this works.

"lan=eng&color=red"

Then you create a temp array.
temp = explode(";");
now temp is
temp[0] = "lan=eng"
temp[1] = "color=red"

Then we're gonna store all these in a hash.
$cookies = array();
hokay so now lets create a cookie hash
foreach ($temp as $var) {
$t = explode("=");
cookie[$t[0]] = $t[1];
}

now to save space we'll unset temp
unset($temp)

Now you have a handy cookie hash.



:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 03-08-2002 06:12

Um. Okay. I must be a little slow...


Wouldn't the first explode be
temp = explode("&");

????

And that's just the first question I have...


u-neek
Bipolar (III) Inmate

From: Berlin, Germany
Insane since: Jan 2001

posted posted 03-08-2002 08:00

$HTTP_GET_VARS["lang"] and $HTTP_GET_VARS["color"] contains each value.

$cookie[0]=$HTTP_GET_VARS["lang"];
$cookie[1]=$HTTP_GET_VARS["color"];

And now you have a handy cookie hash .


Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 03-08-2002 08:02

u-neek -

I don't think that method would work for pulling data FROM a cookie, right?

u-neek
Bipolar (III) Inmate

From: Berlin, Germany
Insane since: Jan 2001

posted posted 03-08-2002 08:08

Hmm, sorry.

yes, first you have to explode "&".

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 03-08-2002 08:09

Nope, that method won't work. You have to use code that Bit wrote above...

And yes, first explode should split string on "&" separater (or any other separateor that you used when creating cookie value). Alos, note that you must supply string which should be splitted to explode function:

explode("&", $HTTP_COOKIE_VARS["YourCookie"]);

BTW The same goes for second explode() function call in Bit's code from above...


bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 03-08-2002 17:46

yeah sorry I wrote that without testing it first
The first explode should be on the ampersand (&;

u-neek this method is for writing a bunch of data to one cookie instead of writing a bunch of different cookies. It makes more sense sometimes when writing a bunch of small values to a cookie since there are limitations on how many can be set.

Also I think as of PHP 4.1.0 the HTTP_COOKIE_VARS array has deprecated to the new _COOKIES array for security purposes.



:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 03-08-2002 18:20

Okay - before going further, let me make sure I'm on the same page...

$temp = explode("&");
/*
now $temp is
$temp[0] = "lan=eng"
$temp[1] = "color=red"
*/

// So far, so good.

$cookies = array();
foreach ($temp as $var) {
$t = explode("=");
cookie[$t[0]] = $t[1];
}
unset($temp);

I'm a little stuck on the hash part. Are there some good online resources that won't make my eyes bleed? Or could someone gimme a couple of lines of an explanation? "Hash for dummies"?

I find the concept quite interesting and need to learn it.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 03-08-2002 18:32

a hash is basically an array except instead of numbers you have names (usually called keys).

so (Sorry we've reached the point where I don't have the thread here to look at when I write this) you should now be able to access them like so

$cookies['lang']
$cookies['color']

The really cool thing about them is that you can ref the key and the value. So you can actually get 2 values in one spot

foreach($cookies as $key => $value) {
echo "You have set $key as $value <br>":
}

Oh and this line:
cookie[$t[0]] = $t[1];

Should be (cookies plural and the "$"):
$cookies[$t[0]] = $t[1];

I put a couple of examples up at http://www.bitdamaged.com/testpages/setcookie.php

The first just looks like this (cheap code might break but works in IE) I used the JS redirect since I don't think I can send a header redirect after sending the cookie val (again quickie code I'd clean it up if I needed to really use it)

code:
<?

$value = "lan=eng&col=red";
setcookie("style", $value,time()+3600);
echo("<script>location.href=\"http://www.bitdamaged.com/testpages/getcookie.php\"</script>");

?>



So this redirects you to "getcookie.php"
Which looks like so

code:
<?
$temp = explode("&", $HTTP_COOKIE_VARS['style']);
$cookies = array();

foreach ($temp as $var) {
$t = explode("=", $var);
$cookies[$t[0]] = $t[1];
}

foreach($cookies as $key => $value) {
echo "You have set $key as $value <br>":
}

?>





:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:


[This message has been edited by bitdamaged (edited 03-08-2002).]

deethree
Obsessive-Compulsive (I) Inmate

From: An igloo up north
Insane since: Mar 2002

posted posted 03-19-2002 04:48

Man, I got 1/8 of the way thru this thread and then I stopped understanding it... heh I guess it might be a while before I impliment this... /me runs to php.net

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 03-19-2002 15:39

Yeah, the
$cookies = array();
foreach ($temp as $var) {
$t = explode("=");
cookie[$t[0]] = $t[1];
}
unset($temp);


part has me a little confused, but I got it working, and I'm trying to make heads or tails out of it as well.

deethree
Obsessive-Compulsive (I) Inmate

From: An igloo up north
Insane since: Mar 2002

posted posted 03-19-2002 16:31

I guess I'm having the problems with writing and reading cookies... for some odd reason.

Hmm... time to reread the thread.

I tend to not read enough before trying it... *sigh* such is life.

I'm gonna tackle a single var in a cookie and go exploding after that..

u-neek: would it be a safe assumtion to say you could set the $layout from a drop down form + a submit? (based on the code you posted earlier.)

code:
<form name="theme" method="post" action="<?=$PHP_SELF?>">
<select name="$layout">
<option value="default" selected>Default</option>
<option value="blue">blue</option>
<option value="black">black</option>
</select>
<input type="submit" name="Submit" value="Submit">
</form>



And then use Pugzly'z Switch Code:

code:
switch ($layout){
case blue:
Do stuph;
break;
case green:
Do stuph;
break;
case red:
Do stuph;
break;
default:
Do Stuph;
}



[This message has been edited by deethree (edited 03-19-2002).]

[This message has been edited by deethree (edited 03-19-2002).]

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 03-19-2002 18:51

Pugz, Dethtree,

hmm lemme try to explain
okay after the first explode into temp you have an array that looks like this (this isn't code)

$temp[0] = 'cookie1=value1'
$temp[1] = 'cookie2=value2'
$temp[2] = 'cookie3=value3'
$temp[3] = 'cookie4=value4'

Okay now we want an empty array unlike the previous one this one is an associative array. we'll populate it in the foreach loop.
$cookies = array();

okay now we'll go through the temp array one by one and explode the values lets walk throuhg the first one:

foreach ($temp as $var) {

okay right now $var = 'cookie1=value1' so we explode around the '=' which will return an array with 2 values:

$t = explode("=", $var);

so now we have $t[0] which equals 'cookie1' and $t[1] which equals 'value1' so we are going to make the key for our cookie associative array 'cookie1' and the value 'value1':

cookie[$t[0]] = $t[1]; // or cookie['cookie1'] = 'value1'

and keep looping through the $temp array:

}

The unset is good for just saving some resources and isn't necessary but a good habit. Especially for temp variables it takes the variable out of memory (for example I have a script that writes out all the country codes for a shipping form. It's a huge array and I unset it after using it to save some server resources)

unset($temp);

Does this make more sense?





.:[ The Tao of Steve ]:.
Be Desireless
Be Excellent
Be Gone
...................................

deethree
Obsessive-Compulsive (I) Inmate

From: An igloo up north
Insane since: Mar 2002

posted posted 03-19-2002 19:53

Actually I do understand your previous post(s) I was/am having problems wrapping my mind around cookies for some reason.
[edit: as in setting them and retriveing information from them.] Umm I should watch what I post seenig as I cannot delete my own posts.. ]

[Edit2:

code:
header
$var = $HTTP_COOKIE_VARS['var']

setcookie
$exptime = gmdate("l, d-M-Y H:i:s \G\M\T", mktime (0,0,0,gmdate("m"), gmdate("d"), gmdate("Y")+1));
Header ("Set-Cookie: var=". $var . "; path=/; expires=". $exptime .";");



So I talked to my buddy... he burped out some auth coding he has done.. and I snagged the above out... nipped it a bit..
He said that he doesn't like setcookie() he prefers the header method.

I'm gonna poke around with this a bit... I hope with his help I can get it... I'll post more as I get it... unless it gets locked from useless posts from people.. /me whistles


I guess the problem that I'm having... where would the code go if my action from the form simply reloads the current page... I don't see how I can add it to the form... unless I set the $var and reload the page... but that seems a little too simple.. I mean.. it has to be hard doesn't it? this is PHP after all.

[This message has been edited by deethree (edited 03-19-2002).]

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 03-19-2002 20:41

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 03-19-2002 21:06

Bitdamaged - that DOES help. Thanks. I was kinda thinking that's the way it was, but I'm really on a LEARN vs. COPY/PASTE mission.

Thanks for the help. Send me a bill!

deethree
Obsessive-Compulsive (I) Inmate

From: An igloo up north
Insane since: Mar 2002

posted posted 03-19-2002 21:06

InI that makes total sence to me... So you don't use cookies @ all? Or are you not that far into telling us yet I think that way of switching themes agrees to me a bit more but I think adding cookie code to that somewhere to remember the users preferences would be 'boss' heh

*nudge* *nudge*

Wait a minute... Some people learn differently my buddy Pugzly.

Seeing code in action helps... mind you, you know what your doing to begin with.

Paste code as anyone sees fit.. heh



[This message has been edited by deethree (edited 03-19-2002).]

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 03-20-2002 08:59

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

deethree
Obsessive-Compulsive (I) Inmate

From: An igloo up north
Insane since: Mar 2002

posted posted 03-20-2002 14:10

heh.. as you can see by my post count... I'm just kinda new around these parts... So I missed out on the phun.

[1] 2Next Page »

« BackwardsOnwards »

Show Forum Drop Down Menu