Jump to content

[SOLVED] code help


magnuna

Recommended Posts

Hello!

I'm a complete PHP newbie having some starting problems, so dont laugh about my questions please  ::)

 

For my website I did two language files (en.php and de.php) containing variables for each textblock in the website.

so then i created a dropdown list (langselect.php) where the user can choose his language:

<select size="1" name="language" class="fieldtext" onchange="document.forms[\'lang\'].submit()">
<option selected>--select--</option>
<option value="de">Deutsch</option>
<option value="en">English</option>
</select>
</form>';

 

In my documents I thought about including the languagefiles like this:

<?php include ("language/$language.php") ?>

 

and the dropdown on the place where it should appear like this:

 <?php include ("langselect.php") ?>

 

And there I got stuck! How do I get this running??

 

Thanks for helping a confused beginner  :-[

 

 

Link to comment
Share on other sites

I'm kind of confused as well. You have Deutsch and English files and you want to include one or the other on your page?

 

Does a simple

if($_POST['language'] == "de"){

include_once("language/de.php");

}

else if($_POST['language'] == "en"){

include_once("language/en.php");

}

 

work?

 

I assume you have an opening form tag that you forgot to copy/paste but if not then you need one!

 

 

Link to comment
Share on other sites

Hello!

Yes thank you, thats almost it!!

 

As the default language should be "de" and the variable $_POST['language'] is empty at the beginning, I thought about adding this code:

 

if($_POST['language'] == "de" or $_POST['language'] == ""){
include_once("language/de.php");

}
else if($_POST['language'] == "en"){
include_once("language/en.php");

}

 

Now the problem is the following:

The website shows a navigation panel. So if the user klicks on the menu to open another page, the $_POST['language'] is empty again.

This includes then the "de.php" language file again, even if the user has chosen "en" as his language.

Is there a possibility to "transfer" the content of $_POST['language']?

 

Thanks a lot for your help!!

Link to comment
Share on other sites

An easier solution might be this. It allows for expansion by just adding the language file in the folder. Theoretically you could have a large list of languages and it would look ugly to have all those if statements all over the place.

 

// get language and set default to de
$language = !isset($_POST['language']) || empty($_POST['language']) ? 'de' : $_POST['language'];

// get the absolute path to the directory containing the language files
$langPath = realpath('includes');
// get the absolute path to the language file we are looking for
$langFilePath = realpath($langPath . DIRECTORY_SEPARATOR . $language . '.php');

// $langFilePath will be false if the file doesn't exist.
// we also need to check that $langFilePath begins with $langPath to protect against RFI attacks
if (!$langFilePath || strpos($langFilePath, $langPath) !== 0) {
// if we made it in here then the file doesn't exist or somebody is trying to mess with the system.
// either way we just say it doesn't exist. we do not wish to expose any information to the people
// trying to mess with the system as that will only help them in further looking for vulnerabilities
throw Exception("The language file '{$language}' could not be found.");
}

// now we can include it
include_once $langFilePath;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.