dqy0707 2012-12-22 13:53
浏览 37
已采纳

会话不改变?

i have problems with the following code that i would like to insert at the top of every page. the main goal is to change language settings. the problem is that when i will post the form, it wont change the session as it should do.

<?php

session_start();

$basename = basename($_SERVER['SCRIPT_NAME'], ".php"); 
$host = $_SERVER['HTTP_HOST'];
$index_path = $host.'/'.$basename;


if (isset($index_path)){
    //header ('Location:http://www.domain.com/'.$pref_language.'/'.$basename);
}
if (!isset($_SESSION['pref_lang'])){ //looks if this session already exists

    if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])){//looks if the browser has set a default language
        $max   = 0.0;
        $languages = explode(",", (strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"])));
        foreach($languages as $language){
            $language = explode(';', $language);
            $q    = (isset($language[1])) ? ((float) $language[1]) : 1.0;
            if ($q > $max){
                $max = $q;
                $pref_language = $language[0];
            }
        }
        $pref_language = trim($pref_language);
    }

    if (!isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])){// in case there is no http_accept_language create it by hand
        $pref_language = "de";  
    }
$_SESSION['pref_lang'] = $pref_language;  //registers the session in case there is no one
$pref_language = $_SESSION['pref_lang'];   //is needed to select the right footer

 }else{   //in case that there is already a session with the saved language

    if  (isset($_REQUEST["fr"])  ) {  //if a new language will be choosen overwrite the old session with the new entry and header to correct path to show the page in the desired language
        $_SESSION['pref_lang'] = 'fr';
        header ('Location: http://www.domain.com/'.$pref_language.'/'.$basename);
    }elseif (isset($_REQUEST["en"])  ) {
        $_SESSION['pref_lang'] = 'en';
        header ('Location: http://www.domain.com/'.$pref_language.'/'.$basename);
    }

    $pref_language = $_SESSION['pref_lang'];  //needed in case that the path is correct to select the right footer version
 }
var_dump($_POST);
var_dump($_SESSION);
echo '<form method="post">      
            <input type="submit" id="en" name="en" value="en"/><div>englisch</div>
        </form>
        <form method="post">  
            <input type="submit" id="fr" name="fr" value="fr"><div>französisch</div>
        </form>';
?>

when

print_r ($_SESSION);

on an other blank page, the session will be the default one "de"?

if there is someone who could help me out, i really would appreciate.

thanks alot.

  • 写回答

1条回答 默认 最新

  • duandi8544 2012-12-22 16:03
    关注

    Try this version and see if it makes sense to you. Be sure to delete your session cookies before the first test ;-)

    <?php // RAY_temp_bonny.php
    error_reporting(E_ALL);
    session_start();
    
    $form = <<<ENDFORM
    <form method="post">
    Choose Language:
    <br/><input type="submit" name="pref_lang" value="de" /><div>german</div>
    <br/><input type="submit" name="pref_lang" value="en" /><div>englisch</div>
    <br/><input type="submit" name="pref_lang" value="fr" /><div>französisch</div>
    </form>
    ENDFORM;
    
    // IF SOMETHING IS POSTED TO REQUEST THE LANGUAGE
    if (!empty($_POST['pref_lang']))
    {
        $_SESSION['pref_lang']  = $_POST['pref_lang'];
        $_SESSION['pref_basis'] = 'POST REQUEST VARS';
    }
    // IF NOTHING IS POSTED TO REQUEST THE LANGUAGE
    else
    {
        // IF THE SESSION PREFERENCE IS NOT ALREADY SET
        if (empty($_SESSION['pref_lang']))
        {
            // IF NO PREFERENCE IN THE REQUEST VARS, CHOOSE GERMAN
            if (!isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]))
            {
                $_SESSION['pref_lang']  = "de";
                $_SESSION['pref_basis'] = 'DEFAULT CHOICE';
            }
            // IF THERE IS A CHOICE IN THE ACCEPT_LANGUAGE
            else
            {
                $_SESSION['pref_lang']  = strtolower(substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2));
                $_SESSION['pref_basis'] = 'HTTP_ACCEPT';
            }
        }
        // IF THE SESSION PREFERENCE WAS PREVIOUSLY SET
        else
        {
            $_SESSION['pref_basis'] = 'PREVIOUS SETTING';
        }
    }
    
    echo " CHOSEN LANGUAGE IS: " . $_SESSION['pref_lang'];
    echo " AND THE REASON IS: "  . $_SESSION['pref_basis'];
    echo $form;
    

    This will use a default value (either "de" or the HTTP_ACCEPT) if no specific language request has been made. If the client has requested the language, it will preserve the requested language in the session.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度