Jump to content


Website Theme Changer


8 replies to this topic

#1 Hyprkookeez

    Young Padawan

  • Members
  • Pip
  • 49 posts
  • Location:Edmonton AB CA
  • Interests:Photoshop, HTML

Posted 28 May 2005 - 09:13 PM

See i have my website, and for v4 I can't please all my friends with orange, So I want to create a couple different themes for my website using stylesheets. I just can't find a tutorial that shows me how to make them and keep the selection with cookies! Can anybody help me out and point me in the right direction to one?

;)

edit: A good example would be http://www.zybez.com/

#2 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 29 May 2005 - 12:08 AM

Use this php

just change the stylesheets
and your domain.
name it theme.php or anything.

<?php
// This array lists the "acceptable" themes
$accept = array('orange','blue','green','lime','pink','purple','red','teal','yellow','etc');

// Get style from a query string
if(isset($_REQUEST['set'])){
	$theme = trim(strip_tags($_REQUEST['set']));
}
else if(isset($_POST['set'])){
	$theme = trim(strip_tags($_POST['set']));
}
else {
    // Unknown request
    $theme = false;
}

// Check if the requested stylesheet is "acceptable"
if(($theme !== false) && (in_array($theme, $accept))){
	setcookie("sitetheme", $theme, time()+31536000, '/', 'domain.com', '0');
}
if(isset($_REQUEST['ref']) || (isset($_POST['ref']))){
    if(isset($_REQUEST['ref'])){ 
        $ref = $_REQUEST['ref'];
    }
    else {
        $ref = $_POST['ref'];
    }
    header("Location: $ref");
    exit;
}
else if(isset($_SERVER['HTTP_REFERER'])){
	// Send the user back to the refering page
	header("Location: ". $_SERVER['HTTP_REFERER']);
	exit;
}
else {
	// No HTTP referrer, send them back to the home page
	header("Location: http://domain.com");
	exit;
}
?>


Then the link for changing it.

<a href="theme.php?set=orange">Change to theme</a>

#3 Hyprkookeez

    Young Padawan

  • Members
  • Pip
  • 49 posts
  • Location:Edmonton AB CA
  • Interests:Photoshop, HTML

Posted 29 May 2005 - 11:55 AM

There has to be some code that you would put in place of the
<link rel="stylesheet" type="text/css" href="themes/style.css">
orelse it would keep using the same stylesheet over and over...?

Or am I wrong.

#4 adam123

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 2,306 posts
  • Location:London, UK
  • Interests:Programming and stuff.

Posted 29 May 2005 - 12:30 PM

Tiki seems to have missed that part out.
Where your <link> tag was, put:
<?php
echo '<link rel="stylesheet" type="text/css" href="themes/' . $_COOKIE['sitetheme'] . '.css" />';
?>

For that to work, you must store all your stylesheets in a folder called 'themes' (without quotes).

#5 Hyprkookeez

    Young Padawan

  • Members
  • Pip
  • 49 posts
  • Location:Edmonton AB CA
  • Interests:Photoshop, HTML

Posted 29 May 2005 - 12:37 PM

Perfect :P I already had them all in a folder called themes.

Except... when someone doesn't have the cookie selected, it doesnt do any stylesheet so my site looks HORRIBLE!

How would i do that with an else... i'm a php noob.

#6 adam123

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 2,306 posts
  • Location:London, UK
  • Interests:Programming and stuff.

Posted 29 May 2005 - 01:15 PM

Ok, at the top of your page (your main page that is), put the following above everything else (including the <html> tag and the doctype, etc.):
<?php

if (!$_COOKIE['sitetheme'])
{
    setcookie ('sitetheme', 'orange');
    header('Location: ' . $_SERVER['PHP_SELF'];
}

?>

That is saying that orange.css is your default template, you can change the 'orange' (keep the quotes) to your default sheet; pink, blue, whatever.

#7 Hyprkookeez

    Young Padawan

  • Members
  • Pip
  • 49 posts
  • Location:Edmonton AB CA
  • Interests:Photoshop, HTML

Posted 29 May 2005 - 02:41 PM

I deleted the cookies, then refreshed the page and it came up with no stylesheet info... I'm going to check the problem.

The source code points to "themes/.css"

... I'm a php noob :)

Maybe if I had a script that created the cookie if it didn't exist right at the top? unless that's what you gave me.

#8 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 29 May 2005 - 03:02 PM

oh sorry about that... heres how i did it

<style>
@import url(library/css/<?php 
	if(isset($_COOKIE['sitetheme'])) {
  echo trim($_COOKIE['sitetheme']);
	} else {echo "lime";} ?>.css);
</style>


#9 Hyprkookeez

    Young Padawan

  • Members
  • Pip
  • 49 posts
  • Location:Edmonton AB CA
  • Interests:Photoshop, HTML

Posted 29 May 2005 - 03:35 PM

Thanks alot tiki and adam123! I got it working!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users