Checking cookie and session value - php

I am working on a multilingual website, and I have a problem now. When the $_GET['setlang'] is empty, language is set to en_US, even if I had ?setlang=pl opened before (which is working fine - language is changing)
empty($_GET['setlang']) ? $_GET['setlang'] = '' : $_GET['setlang'];
switch ($_GET['setlang']) {
case 'en':
$lang = 'en_US';
putenv('LC_ALL='.$lang);
setlocale(LC_ALL, $lang.'.UTF8');
setcookie('lang', $lang, time()+60*60*24*365);
$_SESSION['lang'] = $lang;
$_config['lang'] = $lang;
break;
case 'pl':
$lang = 'pl_PL';
putenv('LC_ALL='.$lang);
setlocale(LC_ALL, $lang.'.UTF8');
setcookie('lang', $lang, time()+60*60*24*365);
$_SESSION['lang'] = $lang;
$_config['lang'] = $lang;
break;
default:
if((isset($_SESSION['lang']) && $_SESSION['lang'] ='en_US') || (isset($_COOKIE['lang']) && $_COOKIE['lang'] = 'en_US')){
$lang = 'en_US';
putenv('LC_ALL='.$lang);
setlocale(LC_ALL, $lang.'.UTF8');
}
else{
$lang = 'pl_PL';
putenv('LC_ALL='.$lang);
setlocale(LC_ALL, $lang.'.UTF8');
}
$_config['lang'] = $lang;
break;
}
$my_name = 'default';
bindtextdomain($my_name,'./locale');
textdomain($my_name);
I can't get this script to change the language to pl_PL after I run ?setlang=pl action. When this get is empty, cookie value is changing to en_US ;/
How should I check it and avoid this problem?
Also I am working with MVC. Does this code seem to be right or should I put some code into model instead of controller?

if((isset($_SESSION['lang']) && $_SESSION['lang'] =='en_US')
|| (isset($_COOKIE['lang']) && $_COOKIE['lang'] == 'en_US'))
make comparisons with == not = this way you assign 'en_US' whatever the $_SESSION["lang"] or $_COOKIE["lang"] is.

Have you started the session? You cannot set or query session variables if you have not.
Use session_start();

$lang = "";
// Check if lang cookie is set
if(isset($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
// Cookie is not set
else
{
// Get language GET variable
$set_lang = (empty($_GET['setlang'])) ? '' : $_GET['setlang'];
switch($set_lang)
{
case 'en':
$lang = "en_US";
break;
case 'pl':
$lang = "pl_PL";
break;
default:
$lang = "en_US";
break;
}
// Set language cookie
setcookie('lang', $lang, time()+60*60*24*365);
}
// Set locale
putenv('LC_ALL=' . $lang);
setlocale(LC_ALL, $lang . ".UTF8");

Related

Change language conditions

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']);
}
}

Converting multi-language function

This is my first ask here so don't judge me if i posted it wrong.
I have this function for my multi-lang support
<?
header('Cache-control: private'); // IE 6 FIX
if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];
// register the session and set the cookie
$_SESSION['lang'] = $lang;
setcookie('lang', $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}
switch ($lang) {
case 'bg':
$lang_file = 'bg.php';
break;
default:
$lang_file = 'en.php';
}
define('IN_VALID',true);
include_once '_src/lang/'.$lang_file;
?>
It gets languages by &lang=en or &lang=bg .
How i can convert it to get &lang:en instead of &lang=en (if it's possible)
?key1=value1&key2=value2 is standard application/x-www-form-urlencoded format.
To use your format, you should parse it on your own, route that I don't advise.
You can check for isset($_GET["lang:en"]).
But this is not to advice. = is the standard separator for keys and values. There's no need to change it.

$_get and session. Can't get it to work

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";
}
?>

PHP Redirect by Browser Language not working!

I change languages using php arrays which makes the URL look like this:
http://alexchen.zxq.net/index.php?lang=es
I added $lang=$_SERVER['HTTP_ACCEPT_LANGUAGE']; in my 'controller' file in default sections, and I choose another language in the preferred languages option (Mozilla Firefox),
but it didn't work.
common.php:
<?php
session_start();
header('Cache-control: private'); // IE 6 FIX
if(isSet($_GET['lang'])) {
$lang = $_GET['lang'];
// register the session and set the cookie
$_SESSION['lang'] = $lang;
setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang'])) {
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang'])) {
$lang = $_COOKIE['lang'];
}
else {
//$lang = 'en'; <-this was previous code
//I tried this:
$lang=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
}
// use appropiate lang.xx.php file according to the value of the $lang
switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
break;
case 'es':
$lang_file = 'lang.es.php';
break;
case 'tw':
$lang_file = 'lang.tw.php';
break;
case 'cn':
$lang_file = 'lang.cn.php';
break;
default:
//$lang_file = 'lang.en.php'; <-this was before
//I also tried this:
$lang=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
}
//translation helper function
function l($translation) {
global $lang;
return $lang[$translation];
}
include_once 'languages/'.$lang_file;
?>
Any suggestions?
$_SERVER['HTTP_ACCEPT_LANGUAGE'] is more complex than just a 2-letter ISO code. Check out this question for a good answer on how to parse HTTP_ACCEPT_LANGUAGE in PHP.

How can I display a language according to the user's browser's language inside this code?

How can I display a language according to the user's browser's language inside this mini-framework for my multilingual website?
Basically, it has to display the default language of the user if there's no cookies.
Example of index.php: (rendered output)
<h2><?php echo l('tagline_h2'); ?></h2>
common.php: (controller of which language to output)
<?php
session_start();
header('Cache-control: private'); // IE 6 FIX
if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];
// register the session and set the cookie
$_SESSION['lang'] = $lang;
setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}
//use appropiate lang.xx.php file according to the value of the $lang
switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
break;
case 'es':
$lang_file = 'lang.es.php';
break;
case 'tw':
$lang_file = 'lang.tw.php';
break;
case 'cn':
$lang_file = 'lang.cn.php';
break;
default:
$lang_file = 'lang.en.php';
}
//translation helper function
function l($translation) {
global $lang;
return $lang[$translation]; }
include_once 'languages/'.$lang_file;
?>
Example of /languages/lang.en.php: (where multilingual content is being stored)
<?php
$lang = array(
'tagline_h2' => '...',
I think you are looking for this
Here is very good class for checking and even getting best match against supported languages:
http://snippets.dzone.com/posts/show/6539

Categories