4 replies [Last post]
Badass
Offline
newbie
Last seen: 17 years 23 weeks ago
Joined: 2004-08-19
Posts: 9
Points: 0

Ok

So I want users to be able to change styles on my site.

I thought the best way of doing this is through php and using sessions/cookies. So I go to http://www.alistapart.com/articles/phpswitch/ and try theirs out. It doesnt work. So, I head onto http://www.devarticles.com/c/a/PHP/Building-a-PHP-Style-Switcher/1/ and have a look at this tutorial but its just way too complicated and doesnt make sense. So, Im not that proficient in PHP. I just want a simple switch over when the user clicks a link on my site. Simple. How do I achieve this. The first example seemed a lot simpler but for some reason it just isnt executing.

Can you help me?

DanA
DanA's picture
Offline
Elder
Last seen: 11 years 38 weeks ago
Timezone: GMT+2
Joined: 2004-08-14
Posts: 1087
Points: 2

PHP Style switching - I need help!

A List Apart switcher usually works fine,
- You have to call the switcher(done with a link on your pages),
- to set a cookie and set a value in the switcher page,
- then redirect to the previous page (it needs an unblocked referrer),
- then test in your pages the value in the cookie and generate the link for the selected stylesheet

Which step did you miss ?

stickmus
Offline
Regular
Last seen: 17 years 29 weeks ago
Joined: 2004-07-08
Posts: 17
Points: 0

PHP Style switching - I need help!

The problem is probably (the usual php one) a register globals issue.

On the page you want to read the style instead of:
<link rel="stylesheet" type="text/css"
media="screen" title="User
Defined Style" href="<?php echo
(!$sitestyle)?'defaultstyle':$sitestyle ?>.css" />

try:
<link rel="stylesheet" type="text/css"
media="screen" title="User
Defined Style" href="<?php echo
(!$_COOKIE['sitestyle'])?'defaultstyle':$_COOKIE['sitestyle'] ?>.css" />

Badass
Offline
newbie
Last seen: 17 years 23 weeks ago
Joined: 2004-08-19
Posts: 9
Points: 0

PHP Style switching - I need help!

DanA wrote:
A List Apart switcher usually works fine,
- You have to call the switcher(done with a link on your pages),
- to set a cookie and set a value in the switcher page,
- then redirect to the previous page (it needs an unblocked referrer),
- then test in your pages the value in the cookie and generate the link for the selected stylesheet

Which step did you miss ?

Step 1 - call switcher - done
Step 2 - set cookie and value - yeah i think ive done that heres the code:

<?php
setcookie ('sitestyle', $set, time()+31536000,
	'/', 'stuweb.cms.gre.ac.uk', '0');
header("Location: $HTTP_REFERER");
?>

I dunno if the address is right though - should this be a prob? Its the url for the server

Step 3 - I think the cookie does this
Step 4 - This isnt happening!

When the page loads it recognises this at the top of my page:

<link rel="stylesheet" type="text/css"
media="screen" title="User
Defined Style" href="<?php echo
(!$sitestyle)?'css/style1':$sitestyle ?>.css" /> 

but when I click on the link 'Style B' which has the following code:
><a href="switcher.php?set=css/style2">Style B</a>

it simply refreshes my page in Internet Explorer and shows me a blank white page in Firefox.

What am I doing wrong?

DanA
DanA's picture
Offline
Elder
Last seen: 11 years 38 weeks ago
Timezone: GMT+2
Joined: 2004-08-14
Posts: 1087
Points: 2

PHP Style switching - I need help!

Step 2
I use
$tmp=getenv("HTTP_REFERER");
header("Location:$tmp");

Then it should redirect if the user doesn't block the referrer

Can you test online if a cookie is set and which is the value ? (it won't work with a local server unless you modify

setcookie ('sitestyle', $set, time()+31536000,  '/', 'stuweb.cms.gre.ac.uk', '0');)
into
setcookie ('sitestyle', $set, time()+31536000,  '/', 'localhost', '0');)

Look in the file cookies.txt in
"user"\Application Data\Mozilla\Firefox\Profiles\????.9ux
or in
Temporary Internet files with IE6

If the cookie has the value set

What does the source show ?

You could use
if(isset($_COOKIE["sitestyle"])){$sitestyle=$_COOKIE["sitestyle"];}else {$sitestyle="default";}
/*echo $sitestyle; die();*//*to debug*/
echo "<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" title=\"User Defined Style\" href=\"$sitestyle.css\" />";