Hi! I want to make my site bilingual, so I made a begining of the script: <form action="jazyk.php" method="POST"> <select name="lang"> <option>polski</option> <option>angielski</option> </select> <input type="submit"> </form> <?php if ($_POST[lang]==polski) { echo'Polski'; } else echo'Angielski' ?> PHP: But can you help me with two things ? 1. How to check web browser language ? 2. How to save chosen language in cookies etc. ? /I'm a newbie in PHP so I want to make this script simple /
There are many ways to do this. One way would be to extract all of the text that needs to be in the local language and split it up into locale-specific include files. Then on your main PHP page, you would have a conditional that would check the browsers language (have a look at $_SERVER['HTTP_ACCEPT_LANGUAGE']) and load the appropriate include file. Main: <?php $language = getLanguage(); switch ($language) { case 'en': include_once('lang/english.php'); break; case 'fr': include_once('lang/french.php'); break; case 'de': include_once('lang/german.php'); break; case 'pl': include_once('lang/polish.php'); break; default: include_once('lang/english.php'); break; } ?> <h1><?php echo WELCOME_MSG; ?></h1> PHP: and an example include would be something like: <?php define('WELCOME_MSG', 'Hello, there'); define('GOODBYE_MSG', 'Goodbye!'); define('THANKS', 'Thank you!'); ?> PHP:
Some additions: $_SERVER['HTTP_ACCEPT_LANGUAGE'] will return a standard two-char language code, like 'en', 'es', 'fr', 'de', etc. You can simply name your files according these codes: <?php $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE']; if(file_exists("lang/{$lang}.php")) include("lang/{$lang}.php"); else include("lang/en.php"); // A default; ?> PHP: Also you can let your users select a language, maybe with some links and $_GET: <?php $lang = (isset($_GET['lang'])) ? $_GET['lang'] : $_SERVER['HTTP_ACCEPT_LANGUAGE']; if(file_exists("lang/{$lang}.php")) include("lang/{$lang}.php"); else include("lang/en.php"); // A default; ?> PHP: Finally, for website-wide config, you can try something with sessions, like replacing $lang with $_SESSION['lang'] or something. Regards.
Both of these are vulnerable to attack. The HTTP_ACCEPT_LANGUAGE is a header that can be faked and $_GET['lang'] can be changed easily... you could make it include anything, for example "../../etc/passwd\0en". Try this above that code, with 'any_file.php' being any file you have in the same directory: $_GET['lang'] = "any_file.php\0en"; PHP: This is why I used getLanguage() - so you could code a safe and secure way to get their language and make it a function like so .
That was only an idea Maybe with: <?php $lang = (isset($_GET['lang'])) ? substr(basename($_GET['lang']), 0, 2) : substr(basename($_SERVER['HTTP_ACCEPT_LANGUAGE']), 0, 2); if(file_exists("lang/{$lang}.php")) include("lang/{$lang}.php"); else include("lang/en.php"); // A default; ?> PHP: Maybe it's more secure Thanks Bye!
That's always the best . You should just preg_match() instead of ereg() though, since it's deprecated.