Multilanguage site works on localhost but not on the Internet - php

I made a website, with 3 lang on it, and site works fine (localhost) but when I upload it, default lang is EN and whenever I change lang on the web, let's say from EN to FR, page goes on that lang in this case on FR and redirect me on home page and all text on site is on FR, that's ok, but after that, when I try to click on let's say about us, then on it's own change it back to default lang, and show me that page (about us) but on default lang...
Here is sample of code how it's look...
This is select.php and I include_once this file on every page...
<?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';
}
switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
break;
case 'fr':
$lang_file = 'lang.fr.php';
break;
case 'de':
$lang_file = 'lang.de.php';
break;
default:
$lang_file = 'lang.en.php';
}
include_once 'lang/'.$lang_file;
This is lang.en.php...
$lang = array();
// menu
$lang['txt1'] = 'Home';
$lang['txt2'] = 'About Us';
$lang['txt3'] = 'Services';
$lang['txt4'] = 'Contact';
It's the same for other two languages...
and I simply put in index.php this...
<ul>
<li><em><b><?php echo $lang['txt1'];?></b></em></li>
<li><em><b><?php echo $lang['txt2'];?></b></em></li>
<li><em><b><?php echo $lang['txt3'];?></b></em></li>
<li><em><b><?php echo $lang['txt4'];?></b></em></li>
</ul>
When is on default lang, I can navigate fine, I can go on any page without problems, but problem is when I try to change site into other language(s),
let's say I'm on page www.example.com/about.php and I want to see this page on let's say FR, site will redirect me, on www.example.com/index.php?lang=fr (this is ok) and then I can see FR lang on site but only on home page, but when I try to go on www.example.com/about.php then site put back default lang, in this case EN...
Strange thing is that this script works fine on localhost...
Any idea why?
Update:
OUTPUT:
Array ( [lang] => lang value to put in session [something_1] => something value to put in session )
Array ( [lang] => new lang value but not set in session [something_1] => something value to put in session )
<?php
session_start();
$lang = "lang value to put in session";
$something = "something value to put in session";
$_SESSION['lang'] = $lang;
$_SESSION['something_1'] = $something;
print_r($_SESSION);
$lang = "new lang value but not set in session";
$something = "new something value but not set in session";
print_r($_SESSION);
PHP shouldn't change the second array
Array ( [lang] => lang value to put in session [something_1] => something value to put in session )
Array ( [lang] => new lang value but not set in session [something_1] => something value to put in session )
It should look like this:
Array ( [lang] => lang value to put in session [something_1] => something value to put in session )
Array ( [lang] => lang value to put in session [something_1] => something value to put in session )
Any idea why?

Be sure your files does NOT echo anything before settings the session
if your saving files as UTF , be sure it's UTF without BOM
disable display_errorsin ini file . try ini_set('display_errors',false);
i hope this helps , i encountered this problem many times .

Related

I can't set a php cookie

I made a php script that allows me to switch the language of my website and for that, I used a cookie called lang to store the user's language prefer but I don't Know why this cookie can't be set as I can't find it in the chrome developer tap .. Although I made this script also with session variable in case cookie isn't set, it doesn't work either, as it shifts the language whenever I change the page
this is my PHP script
<?php
session_start();
if(isset($_GET['lang'])) {
$lan = $_GET['lang'];
$_SESSION['lang'] = $lan;
setcookie("lang", $lan, time() + (3600 * 24 * 30), '/');
} else if (isset($_SESSION['lang'])) {
$lan = $_SESSION['lang'];
} else if (isset($_COOKIE['lang'])) {
$lan = $_COOKIE['lang'];
} else {
$lan = 'en';
}
include "languages/" . $lan . ".php";
?>
I have two files in that language folder en.php and ar.php and each one contains an array with translation like below
<?php
$lang = array(
"Give" => "Give",
"Blood" => "Blood",
"Register as a donator" => "Register as a donator",
"Search for a donator" => "Search for a donator"
I include my script in all my site's pages
here is my chrome developer tap screen shot
any thoughts?
Thanks for helping in advance :)

PHP Session with a Language and Flat-File

Please excuse my English language skills.
I have a Homepage with a language exchange in the following included common.php
<?php
session_start();
header('Cache-control: private');
if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];
$_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 = 'de';
}
switch ($lang) {
case 'en': $lang_file = 'lang.en.php'; break;
case 'de': $lang_file = 'lang.de.php'; break;
case 'ru': $lang_file = 'lang.ru.php'; break;
case 'fr': $lang_file = 'lang.fr.php'; break;
default: $lang_file = 'lang.de.php';
}
include_once 'lang/'.$lang_file;
?>
Now I would like to implement on my website a Mini-Blog with Flat Files and use the following Code
<?php
define("DEFAULT_LANGUAGE", "lang=de");
session_start();
$languages = ["de", "en", "fr", "ru"];
if( isset($_GET["lang"]) ){ $lang = filter_input(INPUT_GET, "lang", FILTER_SANITIZE_STRING);
if( !in_array($lang, $languages) ){ $lang = DEFAULT_LANGUAGE; }
$_SESSION["language"] = $lang;
}
function get_lang() { return isset($_SESSION["language"]) ? $_SESSION["language"] : DEFAULT_LANGUAGE; }
function get_post_names() {
static $_cache = [];
if(empty($_cache)){ $_cache = array_reverse(glob(__DIR__ . '/posts/' . get_lang() . '/*.php')); }
return $_cache;
}
function print_posts() { $paths = get_post_names();
foreach( $paths as $path ){ $content = file_get_contents($path);
echo "<div class='post'>\n";
echo "<p>{$content}</p>\n";
echo "</div>\n\n";
}
}
?>
<?php print_posts(); ?>
For the blog posts, there is a folder [posts] in which subfolders (de, en, fr, ru). In these subfolders there are, depending on the language, for example: "examplepost.php" in whose blog posts have been written.
The language selection works without any problems. But I want that when a user selects a different language, then the Blog automatically reads the voice corresponding folder of the blog and containing the outputs files.
The Problem is that there are two Sessions. This leads to errors! My question would be: How can I merge everything together in a Session or how can I connect the two Sessions without conflicts, so it works both (the language of the site and the output of the files in the folders of the visitors selected language for the Blog posts).
Many thanks in advance to all of you for your help and suggestions! Greeting
You do not need two sessions to hold the same variable ($lang).
What you can do is something like this:
session_start();
// check/set default language session
if (!isset($_SESSION["language"])) {
$_SESSION["language"] = DEFAULT_LANGUAGE;
}
if ($isset($_GET['lang']) && in_array($_GET['lang'], $languages) ){
$_SESSION["language"] = $_GET['lang'];
}
// execute more code
$lang_file = 'lang.'.$_SESSION["language"].'.php';
include_once 'lang/'.$lang_file;
andrew
I think I have expressed myself wrong.
The common.php for the language exchange, is responsible and should be retained. I just need a function in the common.php what are the same functions as in the other mentioned Script. It must be possible, that ...
is changed to the selected language on the website
And at the same time also referred to the sub-folder of the blog ("posts").
The Structure
[root]
|_ acp ........ admin-center
|_ css ........ css styles
|_ img ........ graphics
|_ inc ........ includes files
|_ func ....... function files (i.e. common.php)
|_ js ......... js files
|_ lang ....... language files
|_ posts ...... for the Blog Postings
|_ [de] [en] [fr] [ru]
In the last Folders are .php files witch, depending on the language selection has been selected via the common.php at the same time with the common.php also the files of the [posts] folder of the blog and displayed to be read.
In short ... 1) depending on the language in the language file redirect - AND - 2) in addition to the sub-folder of the [posts] folder and the contained files to be read and the contents of the files output.

Simple switch between lang php with cookies

First I would like to check which language the user is using at the moment (which I have done).
After that if the language isn't the same as the website, overlay with a message that should pop up with question
Do you want change to your language ? click here
The link should redirect to another domain.
If the user choose to be redirected to another domain let's say DE, cookies should keep that settings at least one month and always redirect that user to DE domain even if he enter the main domain.
Here is what I have for now:
<?php
$cookie_name = "user";
$cookie_value = "";
setcookie($cookie_name, $cookie_value, time() + (86400*36) , "/"); // 86400 = 1 day
?>
<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
case "pl":
//echo "PAGE FR";
$domain = 'click here';
break;
case "en":
$domain = 'click here';
break;
default:
$domain = 'click here';
break;
}
HTML
<div class='overlay'>You are using ENG lang atm do you want switch to ?
<?php echo $domain; ?> </div>
First, you have to save the users selection
I would do this with a page between the destination and your current page. For example the link to your one other domain would be http://example.com/changelang.php?target=domain1
there you save the cookie with the desired language
if(isset($_GET['target']) AND $_GET['target'] == 'domain1')
{
setcookie($cookie_name, "domain1", time() + (86400*36) , "/"); // 86400 = 1 day
and the redirect to this page
header('Location: http://de.domain1.com/');
exit;
}
Second, you have to check if there is an existing cookie
<?php
if(isset($_COOKIE[$cookie_name]))
{
//do switch here
switch($_COOKIE[$cookie_name])
{
case 'de':
//redirect to other page
header('Location: http://de.domain1.com/');
exit;
break; //this is useless
}
}

Redirect User Depending on Language

I have the following code:
<?php
// List of available localized versions as 'lang code' => 'url' map
$sites = array(
"da" => "http://www.mysite.com/",
);
// Get 2 char lang code
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// Set default language if a `$lang` version of site is not available
if (!in_array($lang, array_keys($sites)))
$lang = 'en';
// Finally redirect to desired location
header('Location: ' . $sites[$lang]);
?>
This will redirect the user to the Danish (da) version of the site, which is the main site, if it's a Danish website client. This is excellent.
But, I want, if the user isn't Danish, but Polish, German, etc. it redirects them to the English version of the site, which is located at the subdomain http://en.mysite.com/
How do I implement that into the existing code? Thanks in advance!
- Frederick Andersen
EDIT
A solution like;
$sites = array(
"da" => "http://www.mysite.com/",
"en" => "http://en.mysite.com/"
);
Doesn't work since it creates a loop error when redirecting - at least in Google Chrome.
EDIT 2
session_start();
if (isset( $_SESSION['redirect']))
{
// do nothing / continue with rest of page
}
else
{
$_SESSION['redirect'] = true;
// List of available localized versions as 'lang code' => 'url' map
$sites = array(
"da" => "http://www.mysite.com/",
"en" => "http://en.mysite.com/"
);
// Get 2 char lang code
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// Set default language if a `$lang` version of site is not available
if (!in_array($lang, array_keys($sites)))
$lang = 'en';
// Finally redirect to desired location
header('Location: ' . $sites[$lang]);
exit();
}
You would need to add the default option to your array:
$sites = array(
"da" => "http://www.mysite.com/",
"en" => "http://en.mysite.com/"
);
Edit: If you are calling this same code in "http://en.mysite.com/" again, it will create a loop. The obvious solution would be to not call this code there, but an alternative solution would be to set a session variable to indicate that the language selection has already taken place.
To add that session variable you could do something like:
session_start();
if (isset( $_SESSION['redirect']))
{
// do nothing / continue with rest of page
}
else
{
$_SESSION['redirect'] = true;
// your language selection code with header call
exit();
}
$sites = array(
"da" => "http://www.mysite.com/",
"en" => "http://en.mysite.com/"
);
how about this
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
$sub_domain = ($lang == "da") ? "www" : "en";
$link = "http://".$sub_domain.".mysite.com/";
header('Location: ' . $link);

php Aruba $_session issue

I'm testing this code in Localhost and on Server "Aruba".
In local environment it works perfectly while on Server I dont have the expected session value
When I echo out the $_SESSION['lang'] it outputs :
-the correct country code (Ex.'en') in localhost
-On the Aruba server $_SESSION['lang'] outputs the array named $lang (that you can find on lang.en.php)instead of the needed country code!!
Where am I wrong?
thanks
Luca
my home.php
require_once('/web/htdocs/www.mywebsite.com/home/includes/langSwitcher.inc');
echo $_SESSION['lang'];
[..]
my langSwitcher.inc
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'];
$_SESSION['lang']=$lang;
}
else
{
$lang = 'en';
$_SESSION['lang']=$lang;
}
switch ($lang)
{
case 'en':
$lang_file = 'lang.en.php';
break;
case 'it':
$lang_file = 'lang.it.php';
break;
}
include_once $lang_file;
my lang.en.php
/*
-----------------
Language: Italian
-----------------
*/
$langcode='en';
$lang = array();
$lang['PAGE_TITLE'] = 'pagetitle';
$lang['HEADER_TITLE'] = 'title header ';
$lang['SITE_NAME'] = 'name site';
$lang['HEADING'] = 'title';
It sounds like register_globals may be enabled (although that feature is deprecated). You can find out by running a phpinfo() and looking for the register_globals entry.
Assuming it is enabled, the only solution is to fix it in php.ini (you cannot override register_globals with an ini_set() call).
Well you are using $lang to keep the langcode, but also to store the array information. Perhaps in the langSwitcher.inc you should be using $langcode to store the session?
Because you are also settings the $lang var in the session their. On your server it seems to be using a reference to the $lang file, and therefor outputting the latest content set to $lang (which is the array) and on local it is storing the actual content of $lang.
Anyway, it can be solved by not using the same variable name to store two different items.

Categories