我试图在PHP中创建一个基于会话的小型语言切换脚本。由于某种原因会话条件没有通过,就好像我尝试检查的每个会话都已经设置好了。我错过了什么吗?
<?php
session_start();
echo 'session started';
if ( !isset($_SESSION[ 'language' ]) && empty($_SESSION[ 'language' ]) ) {
echo 'set new session'; // Never reach this point, it seems the browser already has the session in store. Tried changing the session name several times.
redirect();
$_SESSION[ 'language' ] = true;
}
function redirect() {
echo 'running redirect()';
$language = explode( ',', $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
switch( $language[0] ) {
case 'de-DE':
case 'sv-SE':
case 'no-NO':
case 'dk-DK':
header( 'Location: http://sample.com/' . substr($lang, 0, -3) . '/' );
break;
}
}
?>