Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: quick question about COOKIE check

  1. #1
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default quick question about COOKIE check

    I've had a cookie based style switcher on my site for a while now and it works great.

    switcher.php accessed like switcher.php?set=red
    PHP Code:
    <?php
    setcookie 
    ('theirstyle'$settime()+31536000
        
    '/''mydomain.com''0');
    header("Location: $HTTP_REFERER");
    ?>
    I'm changing things and want to be able to change a snippet in multiple locations in the template rather than tracking down the class and making changes to several .css files

    I created the following to try to check if the cookie was set and what it was equal to but I'm missing something because only red.gif is showing if blue or green are set.
    PHP Code:
    <?php
    switch ('theirstyle') {

    case (isset(
    $_COOKIE['theirstyle']) || $set == 'red'):
    echo 
    '<img src="images/red.gif" border="0" alt="alt text here">';
    break;

    case (isset(
    $_COOKIE['theirstyle']) || $set == 'blue'):
    echo 
    '<img src="images/blue.gif" border="0" alt="alt text here">';
    break;

    case (isset(
    $_COOKIE['theirstyle']) || $set == 'green'):
    echo 
    '<img src="images/green.gif" border="0" alt="alt text here">';
    break;
       
    default:
    //.. anything can go here
    break;

    ?>
    Does anything stand out to anyone? Thank you
    Last edited by ?foru; 03-07-2009 at 04:27 AM.

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Instead of using or ( || ) in the if statement, use and ( && ) like so:

    Code:
    <?php
    switch ('theirstyle') {
    
    case (isset($_COOKIE['theirstyle']) && $set == 'red'):
    echo '<img src="images/red.gif" border="0" alt="alt text here">';
    break;
    
    case (isset($_COOKIE['theirstyle']) && $set == 'blue'):
    echo '<img src="images/blue.gif" border="0" alt="alt text here">';
    break;
    
    case (isset($_COOKIE['theirstyle']) && $set == 'green'):
    echo '<img src="images/green.gif" border="0" alt="alt text here">';
    break;
       
    default:
    //.. anything can go here
    break;
    } 
    ?>
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Thank you for taking a look at this. I changed it to && and didn't get anything. Your explanation helps to think this out though because && is looking for both to be TRUE where || only wanted one.

    Not sure what it could be since && didn't work. I know that the cookie is set because I went in and changed the style and the old .css files changed it to the correct images.

  4. #4
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I think this is the best way to do it.

    Refresh your browser twice.

    PHP Code:
    <?php

    $set 
    'blue';

    setcookie('theirstyle'$settime() + 31536000'/');

    $theirstyle = isset($_COOKIE['theirstyle']) ? $_COOKIE['theirstyle'] : '';

    switch (
    $theirstyle) {

        case 
    'red':
            
    $image 'RED <img src="images/red.gif" border="0" alt="alt text here">';
            break;

        case 
    'blue':
            
    $image 'BLUE <img src="images/blue.gif" border="0" alt="alt text here">';
            break;

        case 
    'green':
            
    $image 'GREEN <img src="images/green.gif" border="0" alt="alt text here">';
            break;
           
        default:
            
    $image 'DEFAULT <img src="images/green.gif" border="0" alt="alt text here">';
            break;

    }

    echo 
    $image;

    ?>

  5. The Following User Says Thank You to JasonDFR For This Useful Post:

    ?foru (03-06-2009)

  6. #5
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Your example with the switch initially through me off.I just realized that the switch is totally unnecessary. This is best. I think...

    In this case the value of the cookie will have to be the name of your image or css file minus the extension.

    PHP Code:
    <?php

    $set 
    'green';

    setcookie('theirstyle'$settime() + 31536000'/');

    $thierstyle = isset($_COOKIE['theirstyle']) ? $_COOKIE['theirstyle'] : '';

    // with some validation of $theirstyle
    $image in_array($thierstyle, array('red''blue''green')) ?
        
    '<img src="images/' $thierstyle '.gif" border="0" alt="alt text here" />' '<img src="images/red.gif" border="0" alt="alt text here" />';


    echo 
    $image;

    ?>
    Last edited by JasonDFR; 03-06-2009 at 09:53 AM.

  7. The Following User Says Thank You to JasonDFR For This Useful Post:

    ?foru (03-13-2009)

  8. #6
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Wow, I didn't even realize that was a switch statement. I was so tired when I posted last night that my mind thought those were if statements. I think I really need to start sleeping again at night.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  9. #7
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Amazing! Worked like a charm. J both code pieces worked, but I see that the 2nd one is more compact and an interesting way of approaching it (short and precise). I did read in the past that switch statements weren't really needed for 2 to 3 items, but I do however use them in other areas to serve large sections of content by using echo <<< HTML ... HTML;

    Since I already had the cookie setup in switcher.php with the redirect I went ahead and kept that, and just removed the set cookie portions above...gaining an "auto" refresh when switcher.php redirects back...haha

    Did someone say sleep? thetestingsite I hear you, I've been up the last 2 nights until around 3am and getting back up at 7am

    Thank you both again for taking a look at this. I appreciate it!

  10. #8
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Cool. Glad it works.

    As I continue to get better with PHP I am noticing that my code is becoming more compact and more precise, just like you noticed. So that's great. But when I started, my code was always long and drawn out. I was always thinking, "There must be a better way to do this." It is still a struggle to write compact code, but with practice it does just start to happen.

    This forum is a great tool for practicing. Trying to solve "real world" problems has really helped me learn.

    Maybe you already are, but if not, start trying to separate your PHP code from the HTML as much as you can. Do all the logic at the top of the page and assign the information you need to variables or arrays, then use them in the body ( display ) of your pages. So in the case of your initial switch statement, don't have it echo anything out, rather assign the result to a varaible, then once it comes time to output the html, use the variable where it is needed.

    Good luck!

    J

  11. #9
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    I've learned quite a bit since joining the forums since there is a great knowledge base, and one nice thing about PHP is that a lot of times there are many ways of doing things. Since I've been working with PHP I've tried to keep it separate from the HTML as much as possible so its easier to work with.

    With the code example you provided earlier, I am already starting to use it in ways I've never done before.
    PHP Code:
    $theirstyle = isset($_COOKIE['theirstyle']) ? $_COOKIE['theirstyle'] : '';
    $header_image in_array($theirstyle, array('red''blue''green')) ?
    'images/header_' $theirstyle '.gif' 'images/header_red.gif'
    Since I'm dynamically changing the image I've started to use it in my css as well like so...
    PHP Code:
    #header {background: url(<?php echo $header_image?>); background-repeat: no repeat; height: 90px;}
    Thank you again.

  12. #10
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I don't think php works in a css file.

    The best way to do it, is to put ALL style in CSS files. If you want to have different themes, create other css files, changing only what needs to change for the theme. Then use PHP like you are to change the link to the css file when you output the html.

    PHP Code:
    $theirstyle = isset($_COOKIE['theirstyle']) ? $_COOKIE['theirstyle'] : '';
    $css in_array($theirstyle, array('red''blue''green')) ? $theirstyle '.css' 'red.css'
    Then when you link your css, you could:

    HTML Code:
    <link href="/css/base.css" media="screen" rel="stylesheet" type="text/css" />
    <link href="/css/<?php echo $css; ?>" media="screen" rel="stylesheet" type="text/css" />
    Good luck!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •