I want to make a style switcher that changes the style of the page and yet saves the setting so the user comes in next time will see the setting they left with.

Everything seems to be working except for one thing. The problem is that the user needs to select the desired skin twice before it will change to that particular skin. For example, if I want the skin "Red", I click on Red once, nothing happens, but if i click Red again, the skin is changed to red.

Below is my code, please forgive me if my php code is sloppy (inefficient/buggy) as I just started learning this language this week. Please point out any mistakes or things I can improve on so I can learn from this.

<?php

if (isset($skin)) 
{ 
	switch ($skin) 
    { 
        case "2": 
            $skin = "large.css";
	    setcookie ('chosenskin', $skin, time()+31536000);
            break; 
        case "3": 
            $skin = "red.css";
	    setcookie ('chosenskin', $skin, time()+31536000);
            break; 
        case "4": 
            $skin = "green.css";
	    setcookie ('chosenskin', $skin, time()+31536000);
            break; 
        default: 
       	    $skin = "regular.css"; 
            setcookie ('chosenskin', $skin, time()+31536000);
            break; 
    } 
} 
else 
{ 
    $skin = "regular.css"; 
}

$skin = $_COOKIE['chosenskin'];

?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo $skin ?>" />
<title></title>
</head>
<body>

<a href="index.php?skin=1">Regular font</a></ br>
<a href="index.php?skin=2">Large font</a></ br>
<a href="index.php?skin=3">Red BG</a></ br>
<a href="index.php?skin=4">Green BG</a></ br>
Test sentence

</body>
</html>

Thank you all in advance.

    Try reloading the page at the end of the script:

    header("Location: ".$_SERVER['PHP_SELF']);
    

      the skin value passed shud be accessed as
      $REQUEST['skin']

      when i tried ur peice of code...the if did not evaluate to true and simply went into the else part every time....unless ofcourse there is a part of code where u do
      $skin=$
      REQUEST['skin'] ; and havent posted in here.

        You have to think about how PHP and Cookies work. PHP works on the server side. Cookies are on the client.

        So, let's walk through the process.

        1. The user changes their style choice from "blue" to "chartreuse." They click Submit (or whatever), and send the request to the server.

        2. PHP picks up the choice, and the new page that is sent to the user has a command "attached" to it to set the user's style cookie to "chartreuse." The client's browser dutifully changes that cookie -- but after the page has already been sent to the browser.

        So, you can see that the page loads first (using the old cookie value), then changes the cookie to its new value. Shrike's solution will work, but it will confuse the user when his page reloads twice.

        My suggestion is to use an intermediate page -- "Thanks for changing your style. Click OK to load your new style." Then clicking OK takes them back to the page they were on, with the new style loaded up and ready to go.

          PallaviDalvi, my guess is that he has register_globals turned on, and you do not. That's a good thing (having it OFF), but not the subject of this thread, so I'll leave it alone.

          pkipper, take note -- if you have the option, you should seriously consider turning off register_globals. It will save many future headaches. However, that has nothing to do with your current problem.

            Originally posted by Shrike
            Try reloading the page at the end of the script:

            header("Location: ".$_SERVER['PHP_SELF']);
            

            [/B]

            Hi Shrike, thank you for the quick reply. I get your idea, but for some reason the code above does not work. This sounds awfully strange, but everytime I use the code above, it disconnect myself from the IIS (sorry I am running on a local server using IIS, as it is the only way I can practice PHP and CGI from home), therefore my entire connection would be lost. It means as long as that line of code is there, my entire connection would be lost, and I need to remove that line of code, reconnect my IIS host and everything would be fine again. Same thing whether if I run it via http://localhost/ or via my IP addy by the way.

              Hi BuzzLY, you are right, I have global variables turned on. I read a lot about having them turned on is bad (eg. security, efficiency...etc), but i am leaving them on at the moment just to get my php code to work properly. I have a lot to learn 🙁

              Is it possible if I do not use an intermediate page?

                Using PHP to do the cookies, I don't think so. You may have to have Javascript change the cookie, which can happen immediately whenever they change the style, without a page reload. Of course you may still need to reload the page, depending on what your new style changes (stylesheets, new graphics, etc.).

                But that way you should not need to use an intermediate page.

                Don't forget, you can have the intermediate page do an immediate, automatic redirect, so the user doesn't have to click on anything.

                  Thanks BuzzLY, I will try to get an intermediate page running and see how it goes.

                  On a side note, I like to turn Global_Variables off now. I am a real beginner at this, but just say for my code above in the original post, what would i need to change so they don't rely on Global variables?

                  I mean, once I turned off global variable, the code above doesn't seem to do anything anymore.

                    well i dont know if it helps u in any way...but i did a bit of changes in ur code and it works quiet smoothly for me(without the use of intermidiate pages)....here goes

                    if (isset($_REQUEST['skin']))   
                    {
                    switch ($_REQUEST['skin'])
                    {
                    case "1": $skin = "regular.css"; break; case "2":
                    $skin = "large.css"; break;
                    case 3:
                    $skin = "red.css"; break;
                    case "4":
                    $skin = "green.css"; break;
                    default:
                    $skin = "regular.css";
                    break;
                    }
                    setcookie ("chosenskin", $skin,time()+31536000); } else { if (isset($_COOKIE['chosenskin'])) { $skin=$_COOKIE['chosenskin']; } else $skin=""; }

                    well here i dont pick up the style value from the cookie...as its already present in the variable $skin. but when the user comes back to my site i use the cookie...
                    and like BuzzLY pointed i have register_globals off.

                      Hi PallaviDalvi, your code works flawlessly. My hats off to you. You also explained what I needed to know, as in how to run my above code without relying on global variables.

                      Thank you all for the help, I will continue coding and see how I go.

                      Cheers 🙂

                        pkipper...thanx for the appreciation! it boosted my confidence! 🙂

                          One more question, this is awfully strange. The code below, provided by PallaviDalvi, works perfectly fine in any directory except for root directory.

                          if (isset($_REQUEST['skin']))   
                          {
                          switch ($_REQUEST['skin'])
                          {
                          case "1": $skin = "regular.css"; break; case "2":
                          $skin = "large.css"; break;
                          case 3:
                          $skin = "red.css"; break;
                          case "4":
                          $skin = "green.css"; break;
                          default:
                          $skin = "regular.css";
                          break;
                          }
                          setcookie ("chosenskin", $skin,time()+31536000); } else { if (isset($_COOKIE['chosenskin'])) { $skin=$_COOKIE['chosenskin']; } else $skin=""; }

                          I mean, I can have the same code at http://localhost/hello/ or http://localhost/bob/ or http://locathost/michael/, the code would run fine at all three places. However, if I run the same code at http://localhost/, or at the root directory, then I would get the following:

                          Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\PC\My Documents\WWW\home\index.php:1) in C:\Documents and Settings\PC\My Documents\WWW\home\index.php on line 20

                          Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\PC\My Documents\WWW\home\index.php:1) in C:\Documents and Settings\PC\My Documents\WWW\home\index.php on line 21

                          Why does this happen?

                            just a guess...try
                            ob_start();
                            :
                            the code below
                            :
                            ob_end_flush();

                            from what ur error desc says i guess this shud solve the prob.

                              Hi PallaviDalvi, thanks again. Unfortunately the code did not work. The same error messages still appear. I am quite confused. Not too sure why the same piece of code doesn't work under root directory 🙁

                                well.... sorry to tell u... but it works in my case! probable reason wud be the permissions or some thing. i tried it under my localhost..and it works just like in any other directory.

                                  Um...permission, that is a possibility. I have to go through all the IIS setting again 🙁

                                  Thank you for your inputs anyway! 🙂 I will come back once I have a solution... 🙁

                                    Write a Reply...