How to preserve language status using PHP sessions and cookies
I have this in my index.php:
<?php include_once 'file.php' ?>
then I have <html> some content </html>
and I have this in file.php:
<?php
session_start();
heade开发者_如何学运维r('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 = 'en';
}
switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
break;
case 'de':
$lang_file = 'lang.de.php';
break;
case 'es':
$lang_file = 'lang.es.php';
break;
default:
$lang_file = 'lang.en.php';
}
include_once 'languages/'.$lang_file;
?>
I have those 3 files, I mean lang.en.php an so on...
and it doesn't work, my page is empty, no text, why? What's wrong?
lang.en.php contains
<?php
$h1 ="HOME";
$h2 ="ABOUT US";
$h3 ="CONTACT";
$h4 ="FAQ";
$txt1 = "Here goes txt,Here goes txt,Here goes txt,Here goes txt,
Here goes txt,Here goes txt,Here goes txt,Here goes txt,
Here goes txt, Here goes txt,Here goes txt,Here goes txt.";
?>
I don't know what's wrong with this... What should I change? Can someone help me. Thanks.
You are not printing anything except some HTML. To see the contents of the strings in your lang.en.php's files, you'll need to output them using an echo
language construct.
<?php
echo $txt1;
?>
would output:
Here goes txt,Here goes txt,Here goes txt,Here goes txt, Here goes txt,Here goes txt,Here goes txt,Here goes txt, Here goes txt, Here goes txt,Here goes txt,Here goes txt.
Problems like these effectively never occur when you use proper code structuring: indent control structures, haven controllers (logic) and views (presentation). You are mixing up a lot stuff there in one file which makes it obviously difficult for you and others to read and understand the code. Please try to code with readability/maintainability in mind, even if you are using php.
I tested you code (it's from Bitrepository tutprial right :)
Ok, the file.php
should be in your function.php
if ever you have one.
I assume you had index.php
and it echoed those variables from your language files right like:
echo $h1;
echo $h2;
echo $h3;
echo $txt1;
To change language, see your file.php
, it's specified in the cases. If you want english:
index.php?lang=en
index.php?lang=de
index.php?lang=es
And so on.
Easy right \(^_^)/
Check bitrepository for much detailed tutorial. I also would like to suggest to use define()
for your language files.
ex.
define('TITLE', 'This is the title')
To echo:
echo TITLE;
You must define array in lang.en.php, lang.ru.php...
lang.en.php
$lang = array();
$lang["PAGE_TITLE"] = "Some title";
$lang["MENU_HOME"] = "Home";
$lang["HOME_CONTENT"] = "Some content";
lang.ru.php
$lang = array();
$lang["PAGE_TITLE"] = "Russian title";
$lang["MENU_HOME"] = "Russian home";
$lang["HOME_CONTENT"] = "Russian content"; ...
Then, output it in your pages.
<?php echo $lang["PAGE_HOME"]; ?>
<?php echo $lang["MENU_HOME"]; ?>
<?php echo $lang["HOME_CONTENT"]; ?>
session_start()
needs to be at the top of your document.
The session must start before something is sent to the browser.
精彩评论