Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

PHP session variable

Hi

I have a website that uses language files for all the text and I want to have a default language and use a form to allow users to select a different language.

I want to store the selected or default language in a session variable.

The code I'm using is in the code section.

The outcome is that the site loads in the default language of english and the page changes to german if the german button is pressed but it changes back to english when you navigate to a different page.

I'm clearly going wrong but I have no idea where so your help is appreciated.

Cheers
Andy
Code to set session variable:
 
if(isset($_POST['language'])) {
    $_SESSION['language'] = $_POST['language'];
}else{
    $_SESSION['language'] = 'english';}
 
Form for user language selection:
 
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <input type="submit" name="language" value="german" />
        	<input type="submit" name="language" value="english" />
            </form>

Open in new window

0
slicethepie
Asked:
slicethepie
  • 4
  • 2
  • 2
1 Solution
 
gamebitsCommented:
You should do it the other way around, check if the session isset if not set the session using the post variable, this way when you get to the next page and the session has been set it will not default to English.
0
 
slicethepieAuthor Commented:
I think you meant like this:



if(isset($_SESSION['language'])) {
    $_SESSION['language'] = $_POST['language'];
}else{
    $_SESSION['language'] = 'english';}




Which gives me this as a result:

Undefined index: language in

I'm guessing this is because the form hasn't been submitted yet
0
 
gamebitsCommented:
I was think more like


if(!isset($_SESSION['language'])) {
    $_SESSION['language'] = $_POST['language'];
}

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
slicethepieAuthor Commented:
Cool, it works. Thanks

How does it know what the default language is though?
0
 
slicethepieAuthor Commented:
I was wrong, it doesn't work it just had the variable stored from earlier in my session

variable is just empty so the site can't load the language file
0
 
racmail2001Commented:
can you try this?
if(!isset($_SESSION['language'])) {  $_SESSION['language'] = 'english';}
// if language is not set - go with english
if (isset($_POST['language'])){    $_SESSION['language'] = $_POST['language'] ;}
// if you have post for language, change it with hat user selected

Open in new window

0
 
racmail2001Commented:
i hope my spelling it's corect
0
 
slicethepieAuthor Commented:
Works perfectly, thank you
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now