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.

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?

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.

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.

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.

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:

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:

$_SESSION['my_theme'] = 'theme_id';

Checking to see if the session exists is also straight forward:

if(isset($_SESSION['my_theme'])){
    // session exists
    echo $_SESSION['my_theme'];
}

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:

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.

I was affraid you would say that. :slight_smile:
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.