SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    377
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Session Variable Issues

    Hi
    I am trying to get a session variable to determine a file include on my site.
    Depending on the value of the session variable I want the page to select the correct file to include.
    I have the file set up so that it sets the session variable initially when the page is loaded but the user can change the value of this variable by clicking a link.
    This part seems to work fine.
    the part I can't seem to work out though is how to get the include file to change based on the session variable value.

    Here's the code I'm using to set the session variable:

    PHP Code:
    session_start(); // start up your PHP session!
    if ( isset($_GET['Local']) ) { $_SESSION['Local'] = $_GET['Local'];
    }
    else {
    $_SESSION['Local']=1;
    }
    include(
    ''.$Translate.''); 

    Here is the code that doesn't seem to be working to select the correct include file based on the session value:
    PHP Code:
    if ($_SESSION['Local'] !=""){
    $Translate =  "Languages/English.php";}
    elseif (
    $_SESSION['Local'] =="1"){
    $Translate =  "Languages/English.php";}
    elseif (
    $_SESSION['Local'] =="2"){
    $Translate =  "Languages/French.php";}
    elseif (
    $_SESSION['Local'] =="3"){
    $Translate =  "Languages/Spanish.php";}
    elseif (
    $_SESSION['Local'] =="4"){
    $Translate =  "Languages/German.php";}
    elseif (
    $_SESSION['Local'] =="5"){
    $Translate =  "Languages/Italian.php";}
    else 
    $Translate =  "Languages/English.php"
    Does anyone have any idea why this is not working and how to get it to work.
    As always any help is much appreciated.

    Thanks in adavance

  2. #2
    SitePoint Member
    Join Date
    Jun 2007
    Location
    Gold Coast, Australia
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its a bit hard to understand how your script is layed out in the code supplied so it could be to do with the order in which the code is executed. You would need to post the full script to provide us with a better understanding of the structure.

    Personally, I would structure it something like this:

    PHP Code:
    session_start();
    $translations = array(=> 'English'=> 'French'=> 'Spanish'=> 'German'=> 'Italian');

    if(isset(
    $_GET['Local']) AND isset($translations[$_GET['Local']])){
        
    $_SESSION['Local'] = $_GET['Local'];
    }elseif(!isset(
    $_SESSION['Local']){
        
    $_SESSION['Local']=1;
    }

    require(
    'Languages/'.$translations[$_SESSION['Local']]); 
    Note: I have left of the . php in the require statement as the forum software seems to think i am posting a link which i am restricted from doing given my limited posts.

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Here's what I would do:
    PHP Code:
    <?php
    Session_Start
    ();
    if(!
    Array_Key_Exists('Language'$_SESSION)) $_SESSION['Language'] = 'English';
    if(
    Array_Key_Exists('Language'$_GET) && Preg_Match('/^[A-Za-z\d\-]+$/'$_GET['Language']) && File_Exists("Languages/{$_GET['Language']}.php")){
        
    $_SESSION['Language'] = $_GET['Language'];
    }
    $TranslateFile "Languages/{$_SESSION['Language']}.php";
    ?>
    That first sets the default if the session doesn't exist. If a session value already exists, it sticks to that unless a $_GET value is there. If the $_GET value is there, it first checks that the string is alphanumeric (accepts a dash too), and that the file exists. If both of those conditions are satisfied, it sets the language.

    It's important to check that certain characters aren't included because using something like '?Language=../index.php' could try and require() Languages/../index.php, which is the same as just index.php. By making sure there are no dots or slashes, you're restricting them to the current directory - plus no language name would have those characters.

    Off Topic:

    Notice that I used the variable 'Language'. It's important to keep your variables well-named, and the variable 'Language' is more suitable as it's reason is for the language. I live in Wales so my 'local' value, by definition, would be 'Welsh'. However, I prefer to read and write in English (because I'm not fluent in Welsh), so my LANGUAGE would be 'English'.

    Yeah, it doesn't really matter in the end, but it's nice to keep your variables well named, especially when you may want to make big changes a few months down the line... Looking at 'Local' you'd think 'What's that?', but looking at 'Language', it's obvious!
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    377
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's great thanks very much for the help once again.


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
  •