在我的网站上,用户有几个选项,如语言,日期和地区。当用户点击其中任何一个都很好时,网站会适应特定的选择,但是当他们去另一个页面时他们放弃了所有的选择,如果我找到一种方法来保留他们的整个访问或者也许24小时。
示例:bob进入我的网站,在区域设置下,他点击英国,因此网站适应并向他显示英国国旗的结果。然而,当他进入下一页时,他失去了旗帜和他的地区设置,并且必须重新选择他的地区设置。
这就是我想解决的问题。我将发布整个代码以防这可能有所帮助:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Loughborough University | Students Union</title>
<script type="text/javascript" src="scripts/changesheets.js"></script>
<?php include("scripts/lang_change.php"); ?>
<?php include("scripts/factoid_randomise.php"); ?>
<?php include("scripts/greeting.php"); ?>
<?php include("scripts/menu_change.php"); ?>
<?php include("scripts/lang_select.php"); ?>
<?php include("scripts/geo_loc_change.php"); ?>
<link href="css/style.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/grey_white.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/orange_black.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/offwhite_blue.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/style.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript">
//Specify affected tags. Add or remove from list:
var tgs = new Array( 'div','td','tr','li','p');
//Specify spectrum of different font sizes:
var szs = new Array( 'xx-small','x-small','small','medium','large','x-large','xx-large' );
var startSz = 2;
function ts( trgt,inc ) {
if (!document.getElementById) return
var d = document,cEl = null,sz = startSz,i,j,cTags;
sz += inc;
if ( sz < 0 ) sz = 0;
if ( sz > 6 ) sz = 6;
startSz = sz;
if (!( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];
cEl.style.fontSize = szs[ sz ];
for ( i = 0 ; i < tgs.length ; i++ ) {
cTags = cEl.getElementsByTagName( tgs[ i ] );
for ( j = 0 ; j < cTags.length ; j++ ) cTags[ j ].style.fontSize = szs[ sz ];
}
}
</script>
</head>
<body>
<div id="wrapper">
<div id="header">
<?php
if(!isset($_POST['languages']))
{
$_POST['languages'] = array('English');
}
foreach ($_POST['languages'] as $language) {
switch ($language) {
case 'Japanese' :
echo "<div id='logo_japanese'></div>";
break;
case 'Chinese' :
echo "<div id='logo_chinese'></div>";
break;
default:
echo "<div id='logo'></div>";
break;
}
}
?>
<ruby style='float:right; margin-top:-80px;'>
<rb><a href="javascript:ts('body',2)"><button style='background-image:url(/misc/FYP/images/textchange.png); width:31px; height:29px; border:none; '></button></a> </rb>
<rb><a href="javascript:ts('body',-1)" ><button style='background-image:url(/misc/FYP/images/textchangeb.png); width:23px; height:29px; border:none;'></button></a></rb>
<rb><button onclick='JavaScript:changeSheets(1)' style='background-image:url(/misc/FYP/images/theme1.png); width:29px; height:29px; border:none; '></button></rb>
<rb><button onclick='JavaScript:changeSheets(2)' style='background-image:url(/misc/FYP/images/theme2.png);width:29px; height:29px;border:none;' ></button></rb>
<rb><button onclick='JavaScript:changeSheets(3)' style='background-image:url(/misc/FYP/images/theme3.png);width:29px; height:29px;border:none;' ></button></rb>
<rb><button style='background-image:url(/misc/FYP/images/Britishflag.png);width:48px; height:20px;border:none;' ></button></rb>
</ruby>
<div class='greeting'>
<?php
if(!isset($_POST['languages']))
{
$_POST['languages'] = array('English');
}
foreach ($_POST['languages'] as $language) {
switch ($language) {
case 'German' :
echo welcome($tz_ger,$greetings_ger);
break;case 'Korean' :
echo welcome($tz_kor,$greetings_kor);
break;case 'Russian' :
echo welcome($tz_rus,$greetings_rus);
break;
case 'Japanese' :
echo welcome($tz_jap,$greetings_jap);
break;
case 'Chinese' :
echo welcome($tz_chi,$greetings_chi);
break;
default:
echo welcome($tz_eng,$greetings_eng);
break;
}
}
?></div>
</div>
<div id="line"></div>
<?php
if(!isset($_POST['languages']))
{
$_POST['languages'] = array('English');
}
foreach ($_POST['languages'] as $language) {
switch ($language) {
case 'German' :
echo makeMenu($links,$tabs_ger,$title);
break;
case 'Korean' :
echo makeMenu($links,$tabs_kor,$title);
break;
case 'Russian' :
echo makeMenu($links,$tabs_rus,$title);
break;
case 'Japanese' :
echo makeMenu($links,$tabs_jap,$title);
break;
case 'Chinese' :
echo makeMenu($links,$tabs_chi,$title);
break;
default:
echo makeMenu($links,$tabs_eng,$title);
break;
}
}
?>
<div id="line"></div>
<?php
if(!isset($_POST['languages']))
{
$_POST['languages'] = array('English');
}
foreach ($_POST['languages'] as $language) {
switch ($language) {
case 'German' :
echo changeGeoLoc($GeoLoc,$details_ger,$geoplugin);
break;case 'Korean' :
echo changeGeoLoc($GeoLoc,$details_kor,$geoplugin);
break;case 'Russian' :
echo changeGeoLoc($GeoLoc,$details_rus,$geoplugin);
break;case 'Japanese' :
echo changeGeoLoc($GeoLoc,$details_jap,$geoplugin);
break;
case 'Chinese' :
echo changeGeoLoc($GeoLoc,$details_chi,$geoplugin);
break;
default:
echo changeGeoLoc($GeoLoc,$details_eng,$geoplugin);
break;
}
}
?>
<?php
if(!isset($_POST['languages']))
{
$_POST['languages'] = array('English');
}
foreach ($_POST['languages'] as $language) {
switch ($language) {
case 'Russian' :
echo "<div id='welcome_pic_russian'>";
break;
case 'Japanese' :
echo "<div id='welcome_pic_japanese'>";
break;
case 'Chinese' :
echo "<div id='welcome_pic_chinese'>";
break;
default:
echo "<div id='welcome_pic'>
";
break;
}
}
?>
<div id="play_feature">
<?php
if(!isset($_POST['languages']))
{
$_POST['languages'] = array('English');
}
foreach ($_POST['languages'] as $language) {
switch ($language) {
case 'German' :
echo selectLang($h_ger,$lang_ger,$values,$h2_ger,$lang_ger2,$countries,$day,$month,$year);
break;case 'Korean' :
echo selectLang($h_kor,$lang_kor,$values,$h2_kor,$lang_kor2,$countries,$day,$month,$year);
break;
case 'Russian' :
echo selectLang($h_rus,$lang_rus,$values,$h2_rus,$lang_rus2,$countries,$day,$month,$year);
break;
case 'Japanese' :
echo selectLang($h_jap,$lang_jap,$values,$h2_jap,$lang_jap2,$countries,$day,$month,$year);
break;
case 'Chinese' :
echo selectLang($h_chi,$lang_chi,$values,$h2_chi,$lang_chi2,$countries,$day,$month,$year);
break;
default:
echo selectLang($h_eng,$lang_eng1,$values,$h2_eng,$lang_eng2,$countries,$day,$month,$year);
break;
}
}
?>
<?php
/*
@author geoPlugin (gp_support@geoplugin.com)
@copyright Copyright geoPlugin (gp_support@geoplugin.com)
*/
require_once('scripts/geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
?>
<div id='menu2' style='width:250px; margin-left:-40px;'>
</div>
<div style="color:#616161; margin-left:-20px;">
<blockquote>‘<?php
if(!isset($_POST['languages']))
{
$_POST['languages'] = array('English');
}
foreach ($_POST['languages'] as $language) {
switch ($language) {
case 'German' :
echo randomFactoid($factoidsg);
break;case 'Korean' :
echo randomFactoid($factoidsk);
break;case 'Russian' :
echo randomFactoid($factoidsr);
break;case 'Japanese' :
echo randomFactoid($factoidsj);
break;
case 'Chinese' :
echo randomFactoid($factoidsc);
break;
default:
echo randomFactoid($factoids);
break;
}
}
?>’</blockquote>
</div>
</font>
</div></div>
<div id="homepage_text">
<?php
if(!isset($_POST['languages']))
{
$_POST['languages'] = array('English');
}
foreach ($_POST['languages'] as $language) {
switch ($language) {
case 'German' :
echo "<p>text</p>";
break;
case 'Korean' :
echo "<p>text.</p> ";
break;
case 'Russian' :
echo "<p>text </p>";
break;
case 'Japanese' :
echo "<p>text</p>";
break;
case 'Chinese' :
echo "<p>text</p>";
break;
default:
echo "<p>text</p>";
break;
}
}
?>
</div></div>
</div>
</body>
</html>
这可能是在cookie /会话或类似的东西可行,但我不知道如何将cookie /会话与我编码的方式结合到此。
欢迎除Cookie或会话之外的任何其他建议。
非常感谢任何帮助。
非常重要的更新 * 在下面的建议之后我尝试了这段代码: *
<?php
session_start(); //put this at the top of your script on all pages that need to remember
if(isset($_POST['languages']))
{
$_SESSION['languages'] = $_POST['languages'];
}?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Loughborough University | Students Union</title>
<script type="text/javascript" src="scripts/changesheets.js"></script>
<?php include("scripts/lang_change.php"); ?>
<?php include("scripts/factoid_randomise.php"); ?>
<?php include("scripts/greeting.php"); ?>
<?php include("scripts/menu_change.php"); ?>
<?php include("scripts/lang_select.php"); ?>
<?php include("scripts/geo_loc_change.php"); ?>
<link href="css/style.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/grey_white.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/orange_black.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/offwhite_blue.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/style.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript">
//Specify affected tags. Add or remove from list:
var tgs = new Array( 'div','td','tr','li','p');
//Specify spectrum of different font sizes:
var szs = new Array( 'xx-small','x-small','small','medium','large','x-large','xx-large' );
var startSz = 2;
function ts( trgt,inc ) {
if (!document.getElementById) return
var d = document,cEl = null,sz = startSz,i,j,cTags;
sz += inc;
if ( sz < 0 ) sz = 0;
if ( sz > 6 ) sz = 6;
startSz = sz;
if (!( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];
cEl.style.fontSize = szs[ sz ];
for ( i = 0 ; i < tgs.length ; i++ ) {
cTags = cEl.getElementsByTagName( tgs[ i ] );
for ( j = 0 ; j < cTags.length ; j++ ) cTags[ j ].style.fontSize = szs[ sz ];
}
}
</script>
</head>
<body>
<div id="wrapper">
<div id="header">
<?php
if(!isset($_POST['languages']))
{
$_POST['languages'] = array('English');
}
foreach ($_SESSION['languages'] as $language) {
switch ($language) {
case 'Japanese' :
echo "<div id='logo_japanese'></div>";
break;
case 'Chinese' :
echo "<div id='logo_chinese'></div>";
break;
default:
echo "<div id='logo'></div>";
break;
}
}
?>
<ruby style='float:right; margin-top:-80px;'>
<rb><a href="javascript:ts('body',2)"><button style='background-image:url(/misc/FYP/images/textchange.png); width:31px; height:29px; border:none; '></button></a> </rb>
<rb><a href="javascript:ts('body',-1)" ><button style='background-image:url(/misc/FYP/images/textchangeb.png); width:23px; height:29px; border:none;'></button></a></rb>
<rb><button onclick='JavaScript:changeSheets(1)' style='background-image:url(/misc/FYP/images/theme1.png); width:29px; height:29px; border:none; '></button></rb>
<rb><button onclick='JavaScript:changeSheets(2)' style='background-image:url(/misc/FYP/images/theme2.png);width:29px; height:29px;border:none;' ></button></rb>
<rb><button onclick='JavaScript:changeSheets(3)' style='background-image:url(/misc/FYP/images/theme3.png);width:29px; height:29px;border:none;' ></button></rb>
<rb><button style='background-image:url(/misc/FYP/images/Britishflag.png);width:48px; height:20px;border:none;' ></button></rb>
</ruby>
<div class='greeting'>
<?php
if(!isset($_POST['languages']))
{
$_POST['languages'] = array('English');
}
foreach ($_SESSION['languages'] as $language) {
switch ($language) {
case 'German' :
echo welcome($tz_ger,$greetings_ger);
break;case 'Korean' :
echo welcome($tz_kor,$greetings_kor);
break;case 'Russian' :
echo welcome($tz_rus,$greetings_rus);
break;
case 'Japanese' :
echo welcome($tz_jap,$greetings_jap);
break;
case 'Chinese' :
echo welcome($tz_chi,$greetings_chi);
break;
default:
echo welcome($tz_eng,$greetings_eng);
break;
}
}
?></div>
</div>
<div id="line"></div>
<?php
if(!isset($_POST['languages']))
{
$_POST['languages'] = array('English');
}
foreach ($_SESSION['languages'] as $language) {
switch ($language) {
case 'German' :
echo makeMenu($links,$tabs_ger,$title);
break;
case 'Korean' :
echo makeMenu($links,$tabs_kor,$title);
break;
case 'Russian' :
echo makeMenu($links,$tabs_rus,$title);
break;
case 'Japanese' :
echo makeMenu($links,$tabs_jap,$title);
break;
case 'Chinese' :
echo makeMenu($links,$tabs_chi,$title);
break;
default:
echo makeMenu($links,$tabs_eng,$title);
break;
}
}
?>
<div id="line"></div>
<?php
if(!isset($_POST['languages']))
{
$_POST['languages'] = array('English');
}
foreach ($_SESSION['languages'] as $language) {
switch ($language) {
case 'German' :
echo changeGeoLoc($GeoLoc,$details_ger,$geoplugin);
break;case 'Korean' :
echo changeGeoLoc($GeoLoc,$details_kor,$geoplugin);
break;case 'Russian' :
echo changeGeoLoc($GeoLoc,$details_rus,$geoplugin);
break;case 'Japanese' :
echo changeGeoLoc($GeoLoc,$details_jap,$geoplugin);
break;
case 'Chinese' :
echo changeGeoLoc($GeoLoc,$details_chi,$geoplugin);
break;
default:
echo changeGeoLoc($GeoLoc,$details_eng,$geoplugin);
break;
}
}
?>
<?php
if(!isset($_POST['languages']))
{
$_POST['languages'] = array('English');
}
foreach ($_SESSION['languages'] as $language) {
switch ($language) {
case 'Russian' :
echo "<div id='welcome_pic_russian'>";
break;
case 'Japanese' :
echo "<div id='welcome_pic_japanese'>";
break;
case 'Chinese' :
echo "<div id='welcome_pic_chinese'>";
break;
default:
echo "<div id='welcome_pic'>
";
break;
}
}
?>
<div id="play_feature">
<?php
if(!isset($_POST['languages']))
{
$_POST['languages'] = array('English');
}
foreach ($_SESSION['languages'] as $language) {
switch ($language) {
case 'German' :
echo selectLang($h_ger,$lang_ger,$values,$h2_ger,$lang_ger2,$countries,$day,$month,$year);
break;case 'Korean' :
echo selectLang($h_kor,$lang_kor,$values,$h2_kor,$lang_kor2,$countries,$day,$month,$year);
break;
case 'Russian' :
echo selectLang($h_rus,$lang_rus,$values,$h2_rus,$lang_rus2,$countries,$day,$month,$year);
break;
case 'Japanese' :
echo selectLang($h_jap,$lang_jap,$values,$h2_jap,$lang_jap2,$countries,$day,$month,$year);
break;
case 'Chinese' :
echo selectLang($h_chi,$lang_chi,$values,$h2_chi,$lang_chi2,$countries,$day,$month,$year);
break;
default:
echo selectLang($h_eng,$lang_eng1,$values,$h2_eng,$lang_eng2,$countries,$day,$month,$year);
break;
}
}
?>
<?php
/*
@author geoPlugin (gp_support@geoplugin.com)
@copyright Copyright geoPlugin (gp_support@geoplugin.com)
*/
require_once('scripts/geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
?>
<div id='menu2' style='width:250px; margin-left:-40px;'>
</div>
<div style="color:#616161; margin-left:-20px;">
<blockquote>‘<?php
if(!isset($_POST['languages']))
{
$_POST['languages'] = array('English');
}
foreach ($_SESSION['languages'] as $language) {
switch ($language) {
case 'German' :
echo randomFactoid($factoidsg);
break;case 'Korean' :
echo randomFactoid($factoidsk);
break;case 'Russian' :
echo randomFactoid($factoidsr);
break;case 'Japanese' :
echo randomFactoid($factoidsj);
break;
case 'Chinese' :
echo randomFactoid($factoidsc);
break;
default:
echo randomFactoid($factoids);
break;
}
}
?>’</blockquote>
</div>
</font>
<a href="http://s06.flagcounter.com/more/7ydE" ><img src="http://s06.flagcounter.com/count/7ydE/bg=FFFFFF/txt=000000/border=CCCCCC/columns=3/maxflags=25/viewers=Visitors/labels=0/pageviews=1/" alt="free counters" style="border:none; margin-left:15px;"/></a></div></div>
<div id="homepage_text">
<?php
if(!isset($_POST['languages']))
{
$_POST['languages'] = array('English');
}
foreach ($_SESSION['languages'] as $language) {
switch ($language) {
case 'German' :
echo "<p> </p>";
break;
case 'Korean' :
echo "<p></p> ";
break;
case 'Russian' :
echo "<p> </p>";
break;
case 'Japanese' :
echo "<p>。</p>";
break;
case 'Chinese' :
echo "<p></p>";
break;
default:
echo "<p></p>";
break;
}
}
?>
</div></div>
</div>
</body>
</html>
哪些工作正常。有什么原因,似乎做了人们告诉我的所有事情?**
答案 0 :(得分:11)
会话可以在这种情况下使用。您可以在会话变量中分配这些值,并可以访问应用程序的所有页面。
$_SESSION['somevar']='someval';
答案 1 :(得分:6)
您应该认真考虑使用 gettext 来获得可管理的本地化支持。您只需调用类似<?php _('Welcome!') ?>
的函数,而不是为每个页面执行条件操作,它将根据您的语言设置自动翻译。
答案 2 :(得分:4)
作为会话:
session_start(); //put this at the top of your script on all pages that need to remember
if(isset($_POST['languages']))
{
$_SESSION['languages'] = $_POST['languages'];
}
然后改变:
foreach ($_POST['languages'] as $language) {
到
foreach($ _SESSION ['languages'] as $ language){
作为Cookie:
if(isset($_POST['languages']))
{
setcookie('language', $_POST['languages'], 7776000) //sets cookie for 90 days, or 7776000 seconds
}
并改变 foreach($ _POST ['languages']作为$ language){
到
foreach ($_COOKIE['languages'] as $language) {
我相信这个答案已经完成了......你打算支持多种语言吗?
答案 3 :(得分:4)
会话或Cookie绝对是最好的方法: 问题是你必须有办法将它与用户连接起来。 从技术上讲,您可以将其存储在数据库中,并在用户的计算机上存储一个cookie,该cookie说明要读取哪个数据库条目,甚至存储用户的ip并将其与用户相关联(尽管这是一种非常不可靠的方法)。但这对你想做的事情来说可能是一种过度杀伤力。
首先,让我们重新格式化您已有的内容。
你有$_POST['languages']
,它实际上是一个包含一个结果的数组。
在这种情况下,您也可以将其设为字符串并将其命名为$_POST['language']
当用户最初选择他的语言时,您还必须记得正确地发布(以单数形式)。
session_start();
我们将检查用户是否选择了一种语言(来自$_POST
变量),如果是,则将其分配给$_SESSION
变量。
if ($_POST['language']) {
$_SESSION['language'] = $_POST['language'];
}
会话变量的功能与任何其他变量相同,但它会在页面之间继续进行,直到会话结束。
这很简单。在您的示例中,您通过查找用户“已发布”的内容来检查语言是什么:
isset($_POST['languages'])
在我们的新代码中,我们将检查刚刚保存的会话变量:
$_SESSION['language']
无需将$_POST['languages']
作为数组,然后使用foreach
循环播放它。
更简单的方法是:
switch($_SESSION['language']) {
case "german": //do german suff
case "english": //do english suff
etc...
}
此外,您首先检查会话标记是否为空,然后放置一个值(如果不是)。
在所有情况之后,您可以跳过该步骤,而不是放在switch() { }
内:
default: //do default language stuff
答案 4 :(得分:2)
<?php
session_start();
if (isset($_GET['lang'])) {
$_SESSION['lang'] = $_GET['lang'];
}
if (isset($_SESSION['lang'])) {
define(LANGUAGE, $_SESSION['lang']);
}
else {
define(LANGUAGE, 'en_GB');
}
include 'languages/'.LANGUAGE.'.php';
?>
答案 5 :(得分:0)
添加代码顶部,写入“session_start();” 之后,每当提交更新时,更新匹配的$ _SESSION数组的索引(您可以选择“region”,“language”,“lng”等名称 - 只要您保持一致,您的选择就由您决定)。然后,无论何时加载页面,都要根据$ _SESSION数组中的值加载它。 另见:http://il2.php.net/manual/en/book.session.php
答案 6 :(得分:0)
在所有相关会话更新之后 - 主要的一点是您如何设置$_SESSION
变量。它表明您是从$_POST
获得的,但代码中没有实际发布值的地方。您提到有人点击更改语言 - 是通过表单提交还是通过URL完成?
在设置语言值后立即添加$_SESSION
变量的转储,并查看它是否实际设置...我感觉你从未到达那行代码,因为你没有$_POST
变量在任何地方定义......
答案 7 :(得分:0)
会话变量的唯一问题是用户无法再更改其设置,如果这是您想要的!但是也应该可以在每个页面上使用隐藏的输入变量。事实上我一直在使用隐藏的输入变量,而且我压缩这个变量,所以它非常快速和安全,而且我也在使用ajax。
答案 8 :(得分:0)
阅读冗长的示例代码非常困难。请减小非工作示例的大小。
PHP会话代码依赖于SESSION ID。 有两种方法可以将这些ID从网页传输到网页:POST和GET。但我能看到的是,你只支持一个,即POST方法。在GET的情况下,您必须将SESSION ID变量SID添加到用于导航的URL。
PHP文档中描述了SESSION ID处理的细节: