Solved

skins using cookies

Posted on 2003-10-22
2
251 Views
Last Modified: 2006-11-17
Hi.  I'm using cookies so that the users on my site may select a 'skin't that they prefer to view.  It works great as it is...but uses querystrings which I'd like to avoid.  Ideally, I would like coloured boxes on every page and each colour represents a new skin.  Once selected it is stored in the cookie.

This is the cookiecheck page which is included on every page....
<?

$total_skins = 14;
$default_skin = 1;

  if (isset($_REQUEST['newskin'])) {
    $newskin=(int)$_REQUEST['newskin'];
    if ( ($newskin<1) OR ($newskin>$total_skins) ) $newskin=$default_skin;
  } elseif (isset($_REQUEST['skin'])) {
    $newskin=(int)$skin;
    if ( ($skin<1) OR ($skin>$total_skins) ) $newskin=$default_skin;
  } else $newskin=$default_skin;

  $skin=$newskin;
  setcookie ('skin', "", time() - 3600);
  setcookie('skin',$newskin,time()+(86400*365),'/');
  setcookie('skin',$newskin,time()+(86400*365),'/','.domesticat.net');
  $skin=$newskin;


    $headervar = "includes/header";
    $footervar = "includes/footer";
    $extension = ".php";
?>

So far...the html is like this with the headers and footers being part of the new skin.

<? include($headervar.$skin.$extension); ?><br>

<a href="index.php?newskin=1">Skin 1</a><br>
<a href="index.php?newskin=2">Skin 2</a><br>

<? include($footervar.$skin.$extension); ?>

How could I adapt this so that we don't use querystrings?

Thanks
:-)
0
Comment
Question by:aspguru28
2 Comments
 
LVL 11

Accepted Solution

by:
shmert earned 125 total points
ID: 9601884
Use javascript to set the skin cookie, with query strings as a fallback mechanism

<script language="javascript">
function useSkin(skin) {
    var nextYear = new Date();
    nextYear.setFullYear(nextYear.getFullYear() + 1);
    document.cookie = 'skin=' + skin + '; expires=' + nextYear;
    return false;
}
</script>

<a href="index.php?newskin=1" onclick="return useSkin(1);">Skin 1</a>

This method will work for both javascript and non-javascript browsers.  If a browser has javascript, the useSkin() function will return false, preventing the anchor tag from loading the new page.
0
 

Author Comment

by:aspguru28
ID: 10055614
Thank you shmert.......sorry to take so long with a response.  I have been trying different methods.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now