Set cookie for css file
I need help with something.
I have 3 different css files for a site. When the visitor goes to the site, i have to pick a random css from the 3, then set a cookie for it for 24h, so that the same css is used during that time.
Can somebody help me with this?
Thank you
Well, you can save the cookie server-side or client-side. Server-side with PHP, ASP, etc. And client-side with javascript.
Check these out and see if they won't help you out:
http://www.w3schools.com/PHP/php_cookies.asp
http://plugins.jquery.com/project/Cookie
I know to save the cookie but i dont know what value to write to save the css link or whatever and how to read it..
Ok, i have this code but it is not working. Can somebody look at this and see what is wrong there?
I am trying to save the css file information for 1 hour.
Code:
<?php
// select css file to use:
$cssFiles = array(
1 => 'red.css',
'green.css',
'blue.css'
);
$thisCssFile = $cssFiles[mt_rand(1, count($cssFiles))];
setcookie("kraumcolor", "$thisCssFile", time()+3600);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Untitled</title>
<link rel="stylesheet" type="text/css" href="<?php echo $_COOKIE["kraumcolor"]; ?>" />
</head>
<body>
<p>This is a test.</p>
</body>
</html>
This has to do with your array. You have only set the $cssFiles[1], but you haven't set any of the other numbers. Here's the correct code:
PHP Code:
$cssFiles = array( 1 => 'red.css' , 2 => 'green.css' , 3 => 'blue.css' );
Yes there was an error there thank you. But it still doesn't work. It gives me a new css everytime i reload the page instead of giving me the same css for 1 hour..
Originally Posted by
Ascendancy
This has to do with your array. You have only set the $cssFiles[1], but you haven't set any of the other numbers. Here's the correct code:
PHP Code:
$cssFiles = array(
1 => 'red.css' ,
2 => 'green.css' ,
3 => 'blue.css'
);
There is nothing wrong with the original method. Both of the following will produce the same array indexing:
PHP Code:
$arr1 = ( 5 => 'five' , 'six' , 'seven' );
$arr2 = ( 5 => 'five' , 6 => 'six' , 7 => 'seven' );
Originally Posted by
igotosleepat2
Yes there was an error there thank you. But it still doesn't work. It gives me a new css everytime i reload the page instead of giving me the same css for 1 hour..
You need to check if the cookie exists before making the css file selection.
PHP Code:
<?php
$cssFiles = array( 1 => 'red.css' , 'green.css' , 3 => 'blue.css' );
$cssKey = (isset( $_COOKIE [ 'kraumcolor' ]) and isset( $cssFiles [ $_COOKIE [ 'kraumcolor' ]])) ?
$_COOKIE [ 'kraumcolor' ] : rand ( 1 , count ( $cssFiles ));
$thisCssFile = $cssFiles [ $cssKey ];
setcookie ( 'kraumcolor' , $cssKey , time () + 60 * 60 * 24 );
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in
Nation
eBookworm.us
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks