2013-12-19 47 views
0

我有一段代碼,應該重定向用戶,如果有會話沒有餅乾..PHP餅乾的問題,不包括語言文件

if (isset($_GET['lang'])) { 
    if (in_array($_GET['lang'], $jezici)) { 
     $lang = $_GET['lang']; 
     // register the session and set the cookie 
     $_SESSION['lang'] = $lang; 
     setcookie("lang", $lang, time() + (3600 * 24 * 30)); 
    } else { 
     $lang = 'hr'; 
     $_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 if (empty($_GET['lang']) || !isset($_GET['lang'])) { 
    $trenutni_file = basename($_SERVER['PHP_SELF']); 
    ?> 
    <META http-equiv="refresh" content="0;URL=http://<?php echo $domena; ?>/<?php echo  $trenutni_file; ?>?lang=hr"> 
    <?php 
    exit(); 
} 
?> 

這是我包括它的index.php

$langArray = include 'lang/'.$lang.'.php'; 

但有時我得到這個錯誤未能包括lang/.php ...這段代碼顯然沒有做正確的..如果在會話中沒有設置cookie,我會將用戶重定向到index.php?lang=hr ...任何幫助?

+0

沒有'include'在發佈的代碼。你確定語言變量是否在執行時被設置? – kero

+0

@ user3002173將示例代碼放入您的問題中,而不是評論。另外,你是否正確使用'session_start()'? –

回答

1

您上一個條件(empty($_GET['lang']) || !isset($_GET['lang']))本身是多餘的,因爲empty已經檢查函數是否已設置,因爲您可以閱讀here。此外,它與第一個條件是多餘的。

而且,你缺少的條件,以確定是否在該情況下,VAR在會話或cookie中設置,它實際上不是空的(它可以被設置爲空字符串,例如)

我會更改爲empty呼叫你所有的isset來電,並添加一個最終的,獨立的條件下,如果$ LANG設置爲赤,無論怎樣,如果它有一個有效值

if (!empty($_GET['lang'])) { 
    if (in_array($_GET['lang'], $jezici)) { 
     $lang = $_GET['lang']; 
     // register the session and set the cookie 
     $_SESSION['lang'] = $lang; 
     setcookie("lang", $lang, time() + (3600 * 24 * 30)); 
    } else { 
     $lang = 'hr'; 
     $_SESSION['lang'] = $lang; 
     setcookie("lang", $lang, time() + (3600 * 24 * 30)); 
    } 
} else if (!empty($_SESSION['lang'])) { 
    $lang = $_SESSION['lang']; 
} else if (!empty($_COOKIE['lang'])) { 
    $lang = $_COOKIE['lang']; 
} 


if (empty($lang) || (!in_array($_GET['lang'], $jezici))) { 
     $trenutni_file = basename($_SERVER['PHP_SELF']); 
     ?> 
     <META http-equiv="refresh" content="0;URL=http://<?php echo $domena; ?>/<?php echo  $trenutni_file; ?>?lang=hr"> 
     <?php 
     exit(); 
    }