Web Analytics Made Easy -
StatCounter Cookie trouble - CodingForum

Announcement

Collapse
No announcement yet.

Cookie trouble

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cookie trouble

    I'm sorry if this sounds extremely novice - but I'm having some trouble setting a simple cookie. I want to allow a user to choose a language, and have the cookie remember that language for various reasons. For my first practice, however, I'm simply building the script using numbers (just so I know it works), and then can make it a little more complex once I have something simple written.

    The page in question will be called cookiexample.php. This is the code for that page.

    Code:
    <TITLE>Cookie Example Page</TITLE>
    <B>Select a number to save:</B><BR>
    <a href="http://www.mysite.com/changenum.php">Number 1</a><BR>
    <a href="http://www.mysite.com/changenum.php?set=2">Number 2</a><P>
    <B>Current Number/Your Selection: </B><?php echo (!$number)?'1':$number ?>
    Basically, the user selects a number to be saved in the cookie, and that number is printed below. It's set so that if no cookie has yet been set (or number one is selected), that it displays a default message of '1' (which, in the complex version, would be English).

    Anyway, the user selects their number, and the cookie is set via changenum.php. The contents of that file is:

    Code:
    <?php 
    $Year =31536000 + time();
    setcookie ('number', $set, $year);
    header("Location: http://www.mysite.com/cookiexample.php"); 
    ?>
    It sets the cookie, and then redirects back to the page - displaying the number (either 1 or 2, 1 as a default or if '1' is selected.)

    The logic is there, but it's not turning out as it is supposed to. The number is set, but when the browser is closed and reopened, the default value is shown, rather than the saved value. I'm also getting an error in the IE taskbar saying that "an object is expected". I'm not sure if that has anything to do with it - but if it worked to perfection, that error wouldn't be there.

    I'd really appreciate some help in sorting out the problem so that I can store the cookie and have the value be called when the browser is reopened (even if that be a week later.) The only real problem with the above is that the cookie seems to die when the browser window is closed...

    Andy
    Last edited by Andy14; Oct 2, 2006, 07:27 AM.

  • #2
    variablenames are case sensitive. so change the
    $Year =31536000 + time();
    into
    $year =31536000 + time();
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

    Comment


    • #3
      Works perfectly, thanks raf!
      Last edited by Andy14; Oct 2, 2006, 08:10 AM.

      Comment


      • #4
        if u wanted to change the language of a page wouldnt it be easier to use
        www.server.com/index.php?lang=fr or en-gb or de... whatever languages u use

        Comment


        • #5
          if you want to learn PHP, then you need to change a few things to your coding style:
          - don't use shorttags (<?) because they wount be enabled on all servers. user <?php instead
          - don't ecpect that register_globals will be set to on, because that to wunt be true on all servers, and creates a security issue if you don't initialize your variables.
          so use $_GET['set'] to get the value from the querystring intead of just $set

          for your actual goal, a switch statement could work but it's not an optimal sollution.
          i think that the most elegant way is to use an array. like

          PHP Code:
          $arr_languages = array(
              
          1=>array('label'=>'english''includefile'=>'english.php'),
              
          2=>array('label'=>'french''includefile'=>'frenchlanguage.php'),
              
          3=>array('label'=>'spanish''includefile'=>'espagnol.php'));

          $arr_selected_languag $arr_languages[$_GET['set']]; 
          you can then get the label with $arr_selected_languag['label'] and the includefile with $arr_selected_languag['includefile']

          by wrapping this into a function you can seperate and reuse the languageselection.

          like
          PHP Code:
          <?php
          // you put this function (and similer ones) in a seperate file (like languagefunctions.php) that you include in all pages where you need to be able to set a language

          function set_language($selected '1'){  // sets the defeaultlanguae to english
             
          $arr_languages = array(
               
          1=>array('label'=>'english''includefile'=>'english.php'),
               
          2=>array('label'=>'french''includefile'=>'frenchlanguage.php'),
               
          3=>array('label'=>'spanish''includefile'=>'espagnol.php'));
             if (
          array_key_exists($selected ,  $arr_languages )) {
                
          setcookie ('number'$selected time()+60*60*24*365); // set cookie for 1 year
                
          return $arr_languages[$selected];
              }else{
                die(
          'Invalid language selection');
              }
          }
          //end of page to include
          ?>
          PHP Code:
          //to use it when he hits a link
          require ('languagefunctions.php');
          $arr_selected_languag set_language($_GET['set']);

          //or when he logs in and you want to set it from the cookie
          $arr_selected_languag set_language($_COOKIE['number']); 
          Last edited by raf; Oct 2, 2006, 09:43 AM. Reason: missing php closing-tag
          Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

          Comment


          • #6
            [deleted]
            Last edited by syosoft; Oct 2, 2006, 10:55 AM.
            Active PHP/MySQL application developer available for immediate work.
            syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready

            Comment

            Working...
            X