【发布时间】:2010-02-28 21:19:43
【问题描述】:
我尝试了以下方法:
基本上,它应该说:如果没有 cookie,则从 Web 浏览器获取 lang(这部分有效,这是 lang.php 的工作)。如果有 cookie,会话将从 cookie 中获取其值。如果没有任何内容,请保留为英文。
session.php:
/* Class constructor */
function Session(){
$this->time = time();
$this->startSession();
}
function cf($filename){//function to clean a filename string so it is a valid filename
$fp = explode('/',$filename);
$num = count($fp);
return $fp[$num-1];
}
/**
* startSession - Performs all the actions necessary to
* initialize this session object. Tries to determine if the
* the user has logged in already, and sets the variables
* accordingly. Also takes advantage of this page load to
* update the active visitors tables.
*/
function startSession(){
session_start(); //Tell PHP to start the session
/* Set referrer page */
if(isset($_SESSION['url'])){
$this->referrer = $search = $this->cf($_SESSION['url']);
}else{
$this->referrer = "/";
}
/* Set current url */
$this->url = $_SESSION['url'] = $this->cf($_SERVER['PHP_SELF']);
/* Set user-determined language: */
//set up languages array:
$langs = array('en','es','zh');
//
if(isset($_GET['lang'])){
if(in_array($_GET['lang'],$langs)){
$this->lang = $_SESSION['lang'] = $_GET['lang'];
setcookie($_SESSION['lang'], time() + (3600 * 24 * 30));
}
}
else if(isSet($_COOKIE['lang'])) {
$_SESSION['lang'] = $_COOKIE['lang'];
}
else {
$_SESSION['lang'] = 'en';
}
}
};
【问题讨论】: