How to add a third language with this specific code format? I have successfully managed 2 languages. I would like to add more than 3 languages. Thank you in advance.
<?php
session_start();
if (!isset($_SESSION['lang']))
$_SESSION['lang'] = "en";
else if (!isset($_GET['lang']) && $_SESSION['lang'] != $_GET['lang'] && !empty($_GET['lang'])) {
if ($_GET['lang'] == "en")
$_SESSION['lang'] = "en";
else if ($_GET['lang'] == "sp")
$_SESSION['lang'] = "sp";}
Related
When I change the language on the page everything changes and starts from the main page ("index.php?lang=en") but the question is, when I am in "about.php" I want to stay on the same page "about.php?lang=en".... who can help me to realize that?
session_start();
if (!isset($_SESSION['lang']))
$_SESSION['lang'] = 'en';
else if (isset($_GET['lang']) && $_SESSION['lang'] != $_GET['lang'] && !empty($_GET['lang'])) {
if ($_GET['lang'] == 'en')
$_SESSION['lang'] = 'en';
else if ($_GET['lang'] == 'ru')
$_SESSION['lang'] = 'ru';
}
require_once "languages/" . $_SESSION['lang'] . ".php";
english
russian
This will give you a good direction and cleaner readable & more secure flow:
<?php
session_start();
$lang = 'en'; // default value
$supportedLanguage = array(
'en' => 'English',
'ru' => 'Russian'
);
$lang = (!empty($_SESSION['lang']) && isset($supportedLanguage[$_SESSION['lang']])) ? $_SESSION['lang'] : $lang;
$lang = (!empty($_GET['lang']) && isset($supportedLanguage[$_GET['lang']])) ? $_GET['lang'] : $lang;
$_SESSION['lang'] = $lang;
require_once ("languages/" . $_SESSION['lang'] . ".php");
$languageLinks = '';
foreach ($supportedLanguage as $key => $value) {
$URL = htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES) . '?lang='.$key;
$languageLinks .= ''.$value.' ';
}
I am trying to change the session to a value of "en" or "no" by clicking on a link so that the correct language is being displayed on a website.
I have 2 variables, $langNO and $langEN. These are variables containing keys and values of each language.
I am using this:
if (!isset($_SESSION['lang'])) {$_SESSION['lang'] = "en";}
if ($_SESSION['lang'] === "no") {$lang = $langNO;}
if ($_SESSION['lang'] === "en") {$lang = $langEN;}
$lang is related to the strings in the text.
Example:
<h1><?php echo $lang['heading']; ?></h1>
To change the session value, I use this in changelang.php:
session_start();
$_SESSION['lang'] = $_GET['lang'];
header("Location:index.php");
When I click on NO to change the language, it just stays on the default language.
I would love to get some feedback for how this can be solved.
Thank you!
I just changed the index code a little bit
index.php
<?php
session_start();
if (!isset($_SESSION['lang'])) {
$lang = "en";
} else {
$lang = $_SESSION['lang'];
}
switch ($_SESSION['lang']) {
case "en":
$lang = $langEN;
break;
case "no":
$lang = $langNO;
break;
}
?>
NO
changelang.php
<?php
session_start();
$_SESSION['lang'] = $_GET['lang'];
header("Location:index.php");
?>
changelang.php
Set the header() in a function
function redirect(url) {
header('Location: '. url);
}
session_start()
$_SESSION['lang'] = $_GET['lang'];
redirect('index.php');
I try to make a 'change languge system' for my script. I already created my class and everything works fine. My problem is next: if I have URL index.php?lang=fr , my website language is changed as long I have ?lang=fr. If I remove ?lang=fr from URL, language is changed too back to english. What is wrong in my conditions ?
P.S: $available_langs is an array with en, fr and ro.
if(isset($_GET['lang']) && $_GET['lang'] != ''){
if(in_array($_GET['lang'], $available_langs)){
$_SESSION['lang'] = $_GET['lang'];
$lang = new Language("languages/");
$lang->setLang($_SESSION['lang']);
} else {
$_SESSION['lang'] = "en";
$lang = new Language("languages/");
$lang->setLang($_SESSION['lang']);
}
} else {
if(isset($_SESSION['lang'])){
$lang = new Language("languages/");
$lang->setLang($_SESSION['lang']);
} else {
$_SESSION['lang'] = "en";
$lang = new Language("languages/");
$lang->setLang($_SESSION['lang']);
}
}
Okay, I got this code build up but i guess it is somehow ugly.
Can this been done better?
if (isset($_GET['lang'])) {
$lang = $_GET['lang'];
if ($lang == 'en') {
$_GET['method']($lang);
}
elseif ($lang == 'nl') {
$_GET['method']($lang);
}
else {
$_GET['method']($lang);
}
}
else {
$lang = '';
$_GET['method']($lang);
}
function GET($name, $default=null)
{
if ( !isset($_GET[$name]) )
return $default;
return $_GET[$name];
}
$method = GET('method'); // Don't forget to error-check $method too, or it will be a major security hole!
$method(GET('lang',''));
It appears that you intend to $_GET['method']($lang) no matter what the value of $lang is. So don't check the value; just determine it, and then use it.
if (isset($_GET['lang'])) { $lang = $_GET['lang']; }
else { $lang = ''; }
$_GET['method']($lang);
Won't
if (isset($_GET['lang'])){
$lang = $_GET['lang'];
if ($lang == 'en'){
$_GET['method']($lang);
} elseif ($lang == 'nl'){
$_GET['method']($lang);
} else {
$_GET['method']($lang);
}
} else {
$lang = '';
$_GET['method']($lang);
}
do exactly the same as
if (isset($_GET['lang'])){
$lang = $_GET['lang'];
$_GET["method"]($lang);
} else {
$lang = '';
$_GET['method']($lang);
}
#Highmastdon: To make the example complete see my correction
Since you're calling $_GET['method']($lang); in every case, including the else, you don't need all those conditions to do the same thing; the code you've given could be written as simple as this:
$lang = isset($_GET['lang']) ? $_GET['lang'] : '';
$_GET['method']($lang);
You could even combine those two lines into one like so:
$_GET['method'](isset($_GET['lang']) ? $_GET['lang'] : '');
...although that is starting to get a bit un-readable.
However, I would echo the point made by #Vilx- that calling a function name that is specified directly from the browser can be a major security risk, so you*really* need to vet the contents of method and lang. (imagine if the url looked like ?method=eval&lang=rm+index.php)
$_GET['method'](isset($_GET['lang']) ? $_GET['lang'] : '');
But please, please please don't do this. You have a massive security hole, if the user specifies method=eval (or any other function that you don't really want called).
Can anyone tell me why this doesn't work?
<?php
$lang = $_get["lang"];
if (($lang == "fr"))
{
session_destroy();
session_start();
$_SESSION['lang'] == "fr";
}
if (($lang == "en"))
{
session_destroy();
session_start();
$_SESSION['lang'] == "en";
}
if (isset($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else
{
$lang = "fr";
}
?>
I just can't seem to get it to work and I tried a lot of different things. Just need a direction to the mistake.
It's running on PHP5 on an Apache server if that's any help.
Even without the session I can't even get the $_get to work. With normally is never the case.
$_SESSION['lang'] == "fr";
two equal signs mean comparision operator.
assignment is = (one equal)
One obvious thing is
$_get["lang"];
Variables are case sensitive in PHP. It must be
$_GET["lang"];
if that doesn't solve your problem, you need to describe in detail what exactly doesn't work.
I have re-factored your code to make it easier to work with in the future. As Pekka says and I asked perhaps you need/want $_GET.
<?php
session_start();
$language = $_GET['lang'];
$allowable_languages = array(
'en',
'fr',
);
if(in_array($language, $allowable_languages)) {
$_SESSION['lang'] = $language;
} else {
$_SESSION['lang'] = 'fr';
}
?>
Updated: In answer to your comment:
<?php
session_start();
if(isset($_GET['lang'])) {
$language = $_GET['lang'];
$allowable_languages = array(
'en',
'fr',
);
if(in_array($language, $allowable_languages)) {
$_SESSION['lang'] = $language;
}
}
if(!isset($_SESSION['lang'])) {
$_SESSION['lang'] = 'fr';
}
?>
U just made a single mistake i.e, u are using wrong syntax it should be $_GET['lang'];
Your code doesn't work because session_start() can't be used directly after session_destroy(). Btw, you don't need to destroy session. Just redefine your variable:
$lang = 'fr';
if(isset($_GET['lang']) && in_array($_GET['lang'], array('fr', 'en'))) $lang = $_GET['lang'];
$_SESSION['lang'] = $lang;
For those who thinks the problem is lower case variable - PHP variables are case insensitive.
You are both right. There was a lot of different error. Both the "=" and "==" problem plus the $_GET case sensitive problem.
Here the code i god to work both the $_GET plus the session.
Thanks a lot. I've spend all night trying to find those error plus think i got errorblind.
<?php
$lang = $_GET["lang"];
if (($lang == "fr"))
{
session_destroy();
session_start();
$_SESSION['lang'] = "fr";
}
if (($lang == "en"))
{
session_destroy();
session_start();
$_SESSION['lang'] = "en";
}
if (isset($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else {
$lang = "fr";
}
?>