SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
Thread: please help - save variable
-
Dec 23, 2001, 09:09 #1
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
-
Dec 23, 2001, 09:19 #2
Are you calling session_start()? You need to do this on every page that needs to write to/read from a session
SeanHarry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
-
Dec 23, 2001, 09:22 #3
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
-
Dec 23, 2001, 09:59 #4
Here ya go:
index.php:
PHP Code:<?php
$language = "English";
session_start();
session_register(language);
?>
<a href="page2.php">Click here</a>
PHP Code:<?php
session_start();
echo "Language saved is $HTTP_SESSION_VARS[language]";
?>Harry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
-
Dec 23, 2001, 10:10 #5
Thanks, I'll go give it a try!
-k
-
Dec 23, 2001, 11:07 #6
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");
?>
-
Dec 23, 2001, 12:36 #7
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.
-
Dec 23, 2001, 13:15 #8
Unregister the variable and then register it again
http://www.php.net/manual/en/ref.session.php
SeanHarry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
-
Dec 23, 2001, 13:35 #9
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