www.webdeveloper.com
Results 1 to 7 of 7

Thread: Set cookie for css file

  1. #1
    Join Date
    Jul 2008
    Posts
    28

    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

  2. #2
    Join Date
    Feb 2007
    Posts
    98
    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

  3. #3
    Join Date
    Jul 2008
    Posts
    28
    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..

  4. #4
    Join Date
    Jul 2008
    Posts
    28
    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>

  5. #5
    Join Date
    Sep 2006
    Location
    MA
    Posts
    494
    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(
      
    => 'red.css',
      
    => 'green.css',
      
    => 'blue.css'
    ); 

  6. #6
    Join Date
    Jul 2008
    Posts
    28
    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..

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,144
    Quote Originally Posted by Ascendancy View Post
    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(
      
    => 'red.css',
      
    => 'green.css',
      
    => 'blue.css'
    ); 
    There is nothing wrong with the original method. Both of the following will produce the same array indexing:
    PHP Code:
    $arr1 = (=> 'five''six''seven');
    $arr2 = (=> 'five'=> 'six'=> 'seven'); 
    Quote Originally Posted by igotosleepat2 View Post
    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(=> 'red.css''green.css'=> 'blue.css');
    $cssKey = (isset($_COOKIE['kraumcolor']) and isset($cssFiles[$_COOKIE['kraumcolor']])) ?
       
    $_COOKIE['kraumcolor'] : rand(1count($cssFiles));
    $thisCssFile $cssFiles[$cssKey];
    setcookie('kraumcolor'$cssKeytime() + 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
  •  
HTML5 Development Center



Recent Articles