SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast kerim's Avatar
    Join Date
    Mar 2001
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    please help - save variable

    I have a very basic question. How can I save a variable by having it set only once in a session? I have a web site that I would like to be bilingual. I have two versions of the web site stored in different folders and all I need to do is set the name of the folder in order to switch languages. However, whenever the page is reloaded it forgets that I have set the folder name.

    Here is my default/index page script:

    <?php

    if (empty ($pg)):
    $pg = "page0";
    endif;

    if (empty ($lg)):
    $lg = "engsite";
    endif;


    $strPagename = $pg;
    $strLanguage = $lg;
    include("includes/$strLanguage/config.inc");
    include("includes/$strLanguage/header.inc");
    include("includes/$strLanguage/menu.inc");
    include("includes/$strLanguage/$strPagename.inc");
    include("includes/$strLanguage/footer.inc");


    ?>

    As you can see, the important variable is $lg. Right now I can set it by doing a $PHP_SELF?lg=chisite <a href...> link, however, whenever a link on that resulting page is called it resets the $lg variable to the default english folder (lg=engsite).

    I don't have any problem with the $pg variable because that is actually specified in all of the links, but the $lg is different because I only want it set once per session!

    Thanks for your help!

    kerim

  2. #2
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you calling session_start()? You need to do this on every page that needs to write to/read from a session

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  3. #3
    SitePoint Enthusiast kerim's Avatar
    Join Date
    Mar 2001
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would it be possible to point me to an example of that command being used in a script? I don't know anything about coding - I just copy from other codes and adapt to my needs! Thanks for your help.

    kerim

  4. #4
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here ya go:

    index.php:

    PHP Code:
    <?php

        $language 
    "English";

        
    session_start();
        
    session_register(language);

    ?>

    <a href="page2.php">Click here</a>
    page2.php:

    PHP Code:
    <?php

        session_start
    ();

        echo 
    "Language saved is $HTTP_SESSION_VARS[language]";

    ?>
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  5. #5
    SitePoint Enthusiast kerim's Avatar
    Join Date
    Mar 2001
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I'll go give it a try!

    -k

  6. #6
    SitePoint Enthusiast kerim's Avatar
    Join Date
    Mar 2001
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    not working

    I don't understand how to use this code to let the user change the variable. It works in the initial state, but doing a

    index.php?lg=chisite

    call doesn't change the language as it did before. Now it sticks to the default variable no matter what I do! Any ideas?

    kerim


    PS: My code looks like this:

    <?php
    if (empty ($lg)):
    $lg = "engsite";
    endif;

    session_start();
    session_register($lg);


    if (empty ($pg)):
    $pg = "page0";
    endif;


    $strPagename = $pg;
    $strLanguage = $HTTP_SESSION_VARS[lg];

    include("includes/$strLanguage/config.inc");
    include("includes/$strLanguage/header.inc");
    include("includes/$strLanguage/menu.inc");
    include("includes/$strLanguage/$strPagename.inc");
    include("includes/$strLanguage/footer.inc");

    ?>

  7. #7
    SitePoint Enthusiast kerim's Avatar
    Join Date
    Mar 2001
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess it is easier to just have two index pages with different sets of values. But I would still be interested in how to do this with just one page if possible. Thanks.

  8. #8
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unregister the variable and then register it again
    http://www.php.net/manual/en/ref.session.php

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  9. #9
    SitePoint Enthusiast kerim's Avatar
    Join Date
    Mar 2001
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I'll play with the
    session_destroy();
    command. There is a lot of info on that page - especially in the discussion. It seems to be a tricky problem for lots of folks.

    kerim


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
  •