SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: PHP and cookies

  1. #1
    Quality not quantity. bo5ton's Avatar
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    654
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    PHP and cookies

    I'd like to develop a wordpress site that revolves around users selecting a particular theme depending on the choise they make.
    So far I've think I have come up with a solution to developing this but need some feedback before I jump in. Basically I just need to know if this is a silly way to go about it or if it might actually be feasible.

    Here is a flowchart of how I believe it will work. Feel free to comment or query me.

    I'm not sure if this is the right place to have this thread or not, but it involved php to begin with so I thought I would start here.

    flowchart.jpg

  2. #2
    SitePoint Zealot Sogo7's Avatar
    Join Date
    May 2011
    Posts
    129
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Very few visitors tend to have javascript disabled and those that do may be using screen reader technology, outputting page text as speech or to a braille display. So fretting about how somebody with JS disabled gets to the images whilst admirable may be a waste of time because their software is going to strip out the images.

    Note: That and a good lightbox script should degrade gracefully when javascript is disabled and still show the image, just without any fancy effects or transitions when a thumbnail or link is clicked.

    There are three other subsets of users who have javascript disabled (entirely or just certain elements) and these are spambots, screen scrapers and those using adblocking browser add-ons, though it's probably a bit to soon for you to worry about them.
    Lovelogic.net Personal Projects Pit - Spammers welcome

  3. #3
    Quality not quantity. bo5ton's Avatar
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    654
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sogo7 View Post
    Very few visitors tend to have javascript disabled and those that do may be using screen reader technology, outputting page text as speech or to a braille display. So fretting about how somebody with JS disabled gets to the images whilst admirable may be a waste of time because their software is going to strip out the images.

    Note: That and a good lightbox script should degrade gracefully when javascript is disabled and still show the image, just without any fancy effects or transitions when a thumbnail or link is clicked.

    There are three other subsets of users who have javascript disabled (entirely or just certain elements) and these are spambots, screen scrapers and those using adblocking browser add-ons, though it's probably a bit to soon for you to worry about them.
    You're probably right, i'm stressing too much about users having Javascript disabled as I don't think most would.

    In terms of the PHP, it may be a loaded question, but is it possible to set a cookie for a wordpress theme on page load? I'm not thoroughly educated in PHP.

    I would just need to use setcookie with a value depending on the image they choose?

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I would not store information about themes in cookies. Better to store that information in a database. If you are using wordpress there should already be a cookie for every user that is logged in. You can create a script that uses the id from that cookie to store information about the theme for each user.

  5. #5
    Quality not quantity. bo5ton's Avatar
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    654
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    This site won't be for users to log in. It will only be an information site and won't have a database of users.

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Imho cookies should only be used to store user specific information (login data, form data, authorizations... stuff like that.) Remember, cookies are stored on the users computer. Imho it's not polite to store data on a users computer when it is not user specific.

    You could use sessions instead.

    I understand though, you dont want to bother users with a login form just so your site can remember which theme they last picked.

    And yes. You can set cookies with php on pageload.

  7. #7
    Quality not quantity. bo5ton's Avatar
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    654
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by brense View Post
    You could use sessions instead.
    I'm not sure how do to this. Can you provide any links for guidance? Would this set a theme via their IP or something?
    Thanks.

  8. #8
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bo5ton View Post
    I'm not sure how do to this. Can you provide any links for guidance? Would this set a theme via their IP or something?
    Thanks.
    It would link the theme to their session id. This means that aslong as their browser is open you can see what theme they selected during that browser session. For example. If i go to your site and select a theme. Then go to google.com and later to sitepoint.com and then return to your site I still get to see the same theme. However. Once I close my browser the information disappears. Sessions are stored on the server instead of the users pc.

    Working with sessions in php is very easy. The most important thing is that you must remember to start each script in which you want to use the session with:
    PHP Code:
    session_start(); 
    http://php.net/manual/en/function.session-start.php

    Once you do that you can simply use the $_SESSION variable to store data:
    PHP Code:
    $_SESSION['my_theme'] = 'theme_id'
    Checking to see if the session exists is also straight forward:
    PHP Code:
    if(isset($_SESSION['my_theme'])){
        
    // session exists
        
    echo $_SESSION['my_theme'];


  9. #9
    Quality not quantity. bo5ton's Avatar
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    654
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that brense. (Welcome to the forums by the way, you've been very busy in the last few days I can see!)

    That is helpful as it gives me an idea of what I may need to do, however the only thing that could be a problem is:
    Quote Originally Posted by brense View Post
    Once I close my browser the information disappears.
    I don't think users would appreciate having to choose a theme every time they visit the site.
    As far as I know the only way to have it so the user's theme is remembered even after they close their browser (without making user's login as well) is to have that information stored in a cookie.

  10. #10
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bo5ton View Post
    I don't think users would appreciate having to choose a theme every time they visit the site.
    I was affraid you would say that.
    And you are right. The only 3 ways I can think of are:
    1) Cookies
    2) Have the users login
    3) Link the theme to the users ip address (this is very dirty and unreliable)

    So in this case cookies might be your best solution indeed.


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
  •