magnuna Posted March 16, 2009 Share Posted March 16, 2009 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 Quote Link to comment Share on other sites More sharing options...
trq Posted March 16, 2009 Share Posted March 16, 2009 You need to explain what your problem is. Quote Link to comment Share on other sites More sharing options...
deerly Posted March 16, 2009 Share Posted March 16, 2009 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! Quote Link to comment Share on other sites More sharing options...
magnuna Posted March 16, 2009 Author Share Posted March 16, 2009 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!! Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted March 16, 2009 Share Posted March 16, 2009 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; Quote Link to comment Share on other sites More sharing options...
POG1 Posted March 16, 2009 Share Posted March 16, 2009 Just use a session or cookie because POST method data doesn't last long. Quote Link to comment Share on other sites More sharing options...
imperium2335 Posted March 16, 2009 Share Posted March 16, 2009 Use session variables and include it on every page in your site. session_start() ; $language = $_SESSION['lang'] ; if($language = "en") { include "english-content.html" ; } else { include "dutch-content.html" ; } (thats off the top of my head) Regards, Tom. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.