Jump to content

Make PHP Remember Your Selected Theme


njb182

Recommended Posts

So my site has 2 styles: light and dark. On the index page you can chose which one you want to use.

 

Is there a script that can remember which one you chose so the next time you visit my index page it will redirect to the style you had last time?

 

I know how to use MySQL if it's going to require it.

Link to comment
Share on other sites

I know how to use MySQL if it's going to require it.

 

Then all you need do is save the users choice into a field related to there login details. next time the user visits or logs in, query that field and get the correct style.

Link to comment
Share on other sites

I know how to use MySQL if it's going to require it.

 

Then all you need do is save the users choice into a field related to there login details. next time the user visits or logs in, query that field and get the correct style.

But there is no log in.
Link to comment
Share on other sites

or you could create a table named 'themepref'

id -> auto inc
ip -> varchar(100)
theme -> varchar(100)

 

and then create a little snipplet that will save their pref to the table when they submit for the first time, or update if they submit the second.

 

Then you can just have a query that will select their ip from the list and insert the theme name into the css syntax. and if mysql_num_rows = 0, you can have it set to a default.

Link to comment
Share on other sites

or you could create a table named 'themepref'

id -> auto inc
ip -> varchar(100)
theme -> varchar(100)

 

and then create a little snipplet that will save their pref to the table when they submit for the first time, or update if they submit the second.

 

Then you can just have a query that will select their ip from the list and insert the theme name into the css syntax. and if mysql_num_rows = 0, you can have it set to a default.

 

But beware as users IPs change.

Link to comment
Share on other sites

or you could create a table named 'themepref'

id -> auto inc
ip -> varchar(100)
theme -> varchar(100)

 

and then create a little snipplet that will save their pref to the table when they submit for the first time, or update if they submit the second.

 

Then you can just have a query that will select their ip from the list and insert the theme name into the css syntax. and if mysql_num_rows = 0, you can have it set to a default.

 

But beware as users IPs change.

 

Yes, but some stay the same :o

 

It's just a suggestion if you don't want to make a cookie

Link to comment
Share on other sites

You could set the them in the cookie and mysql. If the ip does change and the cookie is still there, they will still see the selected theme, or if they delete the cookie and the ip is the same.

 

But if the ip changes and the cookie is deleted, it goes back to the original theme, which isn't that much of a problem is it?

Link to comment
Share on other sites

You could set the them in the cookie and mysql. If the ip does change and the cookie is still there, they will still see the selected theme, or if they delete the cookie and the ip is the same.

 

But if the ip changes and the cookie is deleted, it goes back to the original theme, which isn't that much of a problem is it?

Nope.  :)

 

Could someone give me code to do this? When I said i know how to use mysql, I meant i know how to create databases. Thats about it.  :P

Link to comment
Share on other sites

i helped hi iwth the style changing code...fo anyone who canadd to this:

<?php
$style = $_GET['style'];
if ($style == 'dark') {
require_once ($_SERVER['DOCUMENT_ROOT'].'/en/dark.php');
}

else {
require_once ($_SERVER['DOCUMENT_ROOT'].'/en/light.php');
}
?>

Very short  :)

 

Link to comment
Share on other sites

In your if statement, you would check to see if the cookie is set or if the ip is the same: I don't know how you set up your tables in mysql or what they are called, so adjust it to your needs. Also, assign the ip address to a variable, then check to see what theme the ip address has stored. I'm sure you know how to do that?

 

<?php
if ($_COOKIE['dark'] || $ip) {
    require_once ($_SERVER['DOCUMENT_ROOT'].'/en/dark.php');
}
else if($_COOKIE['light'] || $ip) {
    require_once ($_SERVER['DOCUMENT_ROOT'].'/en/light.php');
}     
?>

 

If you don't know how to do the mysql part, you can read up on SELECT, INSERT, UPDATE, and some functions as mysql_fetch_array at w3schools. I'm writing this in a hurry since I have to go, don't know when I'll be back.

 

Hope this helps you a bit.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.