Solved

PHP session variable

Posted on 2009-07-15
8
196 Views
Last Modified: 2013-12-13
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
Comment
Question by:slicethepie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 28

Expert Comment

by:gamebits
ID: 24859383
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
 

Author Comment

by:slicethepie
ID: 24859414
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
 
LVL 28

Expert Comment

by:gamebits
ID: 24859540
I was think more like


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

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:slicethepie
ID: 24859683
Cool, it works. Thanks

How does it know what the default language is though?
0
 

Author Comment

by:slicethepie
ID: 24859743
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
 
LVL 10

Accepted Solution

by:
racmail2001 earned 50 total points
ID: 24859841
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
 
LVL 10

Expert Comment

by:racmail2001
ID: 24859847
i hope my spelling it's corect
0
 

Author Comment

by:slicethepie
ID: 24860037
Works perfectly, thank you
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question