2010-01-16 89 views
1

我有这个在我的index.php保持语言的地位:如何使用PHP会话和饼干

<?php include_once 'file.php' ?> 后来我<html> some content </html>

,我有这样的file.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 = '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; 
?> 

我有这3个文件,我的意思是lang.en.php等等...

它不起作用,我的网页是空的,没有文字,为什么?怎么了?

lang.en.php包含

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

我不知道什么是错这个做...我应该怎么改? 有人可以帮助我。谢谢。

+1

做什么狄氏lang.en.php文件containe?此外,什么是'$ lang'和'$ lang_file' indeholde如果打印自理? – Wookai 2010-01-16 16:43:53

+0

** **永远保持语言的地位使用会话或饼干。但是,只有使用地址字符串元素。子域(例如en.wikipedia.org)是最好的选择,那么路径或查询字符串。 – 2010-10-17 12:01:06

回答

1

您不打印除HTML以外的任何内容。要查看lang.en.php文件中字符串的内容,您需要使用echo语言结构输出它们。

<?php 
echo $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. 
+0

你的index.php中ATT,为什么我需要这file.php? – bhunter 2010-01-17 19:41:43

+0

喀拉杜ATT的index.php中,你不需要它在file.php。您发布的作品,所以其表示问题的代码很可能与index.php文件。能否请您发表您的index.php文件? – 2010-01-18 04:20:27

0

像这些问题有效地从未当你使用正确的代码结构发生。你在一个文件中混合了很多东西,这使你和其他人阅读和理解代码显然很困难。请尽量在可读性/可维护性方面进行编码,即使您使用的是PHP。

-1

session_start()需要位于文档的顶部。

会话必须在某些内容发送到浏览器之前启动。

0

我测试你的代码(这是从Bitrepository tutprial吧:)

确定,file.php应该在你的function.php如果有的话,你有一个。

我假设你有index.php和它呼应这些变量从您的语言文件的权利,如:

echo $h1; 

echo $h2; 

echo $h3; 

echo $txt1; 

要改变语言,看你file.php,它的情况下,自行指定。如果你想要英语:

index.php?lang=en 

index.php?lang=de 

index.php?lang=es 

依此类推。

易权\(^_^)/

检查bitrepository为尽可能详细的教程。我也想建议你的语言文件使用define()

ex。

define('TITLE', 'This is the title') 

两个回声:

echo TITLE; 
0

你必须定义在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"; ... 

然后,在你的页面输出。

<?php echo $lang["PAGE_HOME"]; ?> 
<?php echo $lang["MENU_HOME"]; ?> 
<?php echo $lang["HOME_CONTENT"]; ?>