SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2008
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Cookie: Clueless, Please Help

    Hello,

    I am wondering if there is a way for me to use a php cookie
    to memorize the template theme HTML (not css).

    Right now, I have it set to grab theme=1 by default, but if
    I call a theme using get (index.php?theme=4), I need a cookie
    to remember the queried theme instead. My mind cannot
    fathom the engineering needed to do this. Can anyone please
    help?

    You can see what I have at the link below, the pull downs
    are javascript, but php seems most appropriate since I''m
    using gueries for the theme. The js pulldown does a query,
    and so PHP needs to see any query and build cookie. http://tinyurl.com/55klxr

    Please and thanks,
    Cal


    PHP Code:
    <?php
    /* 
    Front Controller Script: 
    NOTE PATTERN: index.php?theme=1
    */

    /* Default Theme Template (unless otherwise specified) */ 
    $pgtheme '4'/* 1, 4, OR 24 */

    /* THEME: Include or Static */
    $gttheme = isset( $_GET['theme'] ) ? $_GET['theme'] : $pgtheme;
    $theme=('theme'.$gttheme.'.php'); 

    /* THEME INCLUDE FOR FILL/FORMATTING: All similarly formatted */
    include ($theme); // returns html with variable and includes
    ?>

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Location
    India
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You may use cookies to set / store user preferences and later retrieve the stored value from cookie to set user theme / template.

    Here is a pseudocode ...
    setcookie('template', '1') to set template with value 1
    You may retrieve the value with
    if (isset($_COOKIE['cookie'])) {
    //Do something here ...
    }

    thx
    GS

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php 
    /* 
    Front Controller Script: 
    NOTE PATTERN: index.php?theme=1 
    */ 

    /* Default Theme Template (unless otherwise specified) */ 
    $pgtheme '4'/* 1, 4, OR 24 */ 
    if(isset($_GET['theme']) && in_array(array(1224), $_GET['theme'])){ //if a theme is set and in the allowed array
        
    $pgtheme $_GET['theme']; //set the theme to the requested theme
    }else if(isset($_COOKIE['theme'])){ //else if a cookie exists
        
    $pgtheme $_COOKIE['theme']; //set the theme to it
    }
    setcookie ('theme' $pgthemetime() + (30 24 60 60), '/'); //set the cookie for approx 6 months
    /* THEME: Include or Static */ 
    $theme'theme'.$pgtheme.'.php';

    /* THEME INCLUDE FOR FILL/FORMATTING: All similarly formatted */ 
    include ($theme); // returns html with variable and includes 
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2008
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Arkinstall,

    Thank you very much for your input and assistance. I do appreciate it.

    I'm now with error, "wrong data type in second argument on line 2, the if/else statement. Anyone see the problem? Line one sets default $pgtheme, but if there's a query, it needs to override and set a cookie, which is why I was originally using $gttheme to represent the final.
    Many Thanks,
    Cal

    PHP Code:
    <?php
    $pgtheme 
    '4'/* 1, 4, OR 24: DEFAULT THEME IF NONE SPECIFIED */ 
    if(isset($_GET['theme']) && in_array(array(1224), $_GET['theme'])){ //if a theme is set and in the allowed array 
        
    $pgtheme $_GET['theme']; //set the theme to the requested theme 
    }else if(isset($_COOKIE['theme'])){ //else if a cookie exists 
        
    $pgtheme $_COOKIE['theme']; //set the theme to it 

    setcookie ('theme' $pgthemetime() + (30 24 60 60), '/'); //set the cookie for approx 6 months 
    /* THEME: Include or Static */ 
    $theme'theme'.$pgtheme.'.php'

    /* THEME INCLUDE FOR FILL/FORMATTING: All similarly formatted */ 
    include ($theme); // returns html with variable and includes ?>

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ah, my bad, got the params mixed around in in_array (PHP's so lousy when it comes to keeping needle and haystack in the same order):
    PHP Code:
    if(isset($_GET['theme']) && in_array($_GET['theme'], array('1''2''24'))){ 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2008
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ARKINSTALL,

    You did it again. Works like a charm. Thank You!.
    It's getting cool now, all of those buttons.
    http://tinyurl.com/55klxr

    Is it an easy task to set it for random theme
    within the same array?

    I've never been able to cookie the font-family js
    switcher that I posted here in March. I've never
    found a working one online. The code works, but no
    cookie. Anyone?
    http://www.sitepoint.com/forums/showthread.php?t=545123

    Thanks again Ark,
    Cal


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
  •