2013-05-16 28 views
0

我有一個多維的語言網站。當調用不存在的頁面時,.htaccess將在根方向參考404.php

.htaccess看起來像這樣:

ErrorDocument 404 /404.php 

404.php在根方向簡單地看起來其默認語言是由一個SESSIONCOOKIE設定,指的是語言subdirection /language/404.php

404.php長相像這樣:

include_once "scripts/pref_language.php"; 

if (isset ($_GET['uri']) === true){ 
    $get = $_GET['uri']; 

    header("Location: /$pref_language/404.php?uri=$get"); 
    exit; 
} 

$url = urlencode($_SERVER['REQUEST_URI']); 

header("Location: /$pref_language/404.php?uri=$url"); 

Refered的語言subdirection,張貼URL看起來像這樣:

http://www.example.com/english/404.php?uri=somedata 

或用其他語言:

http://www.example.com/german/404.php?uri=somedata 

/english/404.php的代碼看起來像這樣:

<?php 
error_reporting(E_ALL); 
ob_start(); 
session_start(); 
header ("Content-Type: text/html; charset=utf-8"); 

include_once "../scripts/db_connect.php"; 
include_once "../scripts/functions.php"; 
include_once "../scripts/browser_identification.php"; 
include_once "../scripts/pref_language.php"; 

...some text variables in its specific language 

include_once "../templates/template_404.php"; 
?> 

的模板只是一些HTML造型。將包含在template_404.php中的頁腳是發送數據的主要吸引力。在該頁腳中,可以選擇更改語言設置並在語言路徑之間切換。如果form將被提交,SESSIONCOOKIE將被更改,並且頁面開始標題功能。

因此,這裏是從頁腳代碼:

if (isset($_GET['uri'])) { 
    $uri = urlencode($_GET['uri']); 
    echo "TEST URI: $uri"; 
} 

$en_dir = '../en/'; 
$de_dir = '../de/'; 
$fr_dir = '../fr/'; 
$pl_dir = '../pl/'; 
$tr_dir = '../tr/'; 

... 

$basename = basename($_SERVER['SCRIPT_NAME'], ".php"); 

if ($basename === "index") { 
    $form_action = rtrim($_SERVER['PHP_SELF'], 'index.php'); 
} else{ 
    $form_action = htmlentities($_SERVER['PHP_SELF']); 
} 

... 

if (isset($_POST['pref_lang']) === true) { 

    $content = $_POST['pref_lang']; 

    if ($content === "de") { 

     if ($basename === "about") { 
      $basename = "info"; 
     } else if ($basename === "contact") { 
      $basename = "kontakt"; 
     }   
    } 
    $_SESSION['pref_language'] = $_POST['pref_lang']; 

    setcookie('pref_language', '', time()- 999999, '/', '.example.de'); 
    setcookie('pref_language', $content, time()+60*60*24*365, '/', '.example.de'); 

    if ($basename === "404"){ 

     header("Location: ../$content/404.php?uri=$uri"); 

    } else { 

     header("Location: ../$content/$basename"); 
    } 
} 
?> 
<div id="data"> 
    <fieldset> 
     <ul> 
      <li> 
       <form action="<?php echo $form_action;?>" method="post">  
        <input type="submit" id="de" name="pref_lang" value="de"/><label for="de">german</label> 
       </form> 
      </li> 

      <li> 
       <form action="<?php echo $form_action;?>" method="post"> 
        <input type="submit" id="fr" name="pref_lang" value="fr"/><label for="fr">french</label> 
       </form> 
      </li> 

      <li> 
       <form action="<?php echo $form_action;?>" method="post"> 
        <input type="submit" id="pl" name="pref_lang" value="pl"/><label for="pl">polish</label> 
       </form> 
      </li> 

      <li> 
       <form action="<?php echo $form_action;?>" method="post"> 
             <input type="submit" id="tr" name="pref_lang" value="tr"/><label for="tr">turkish</label> 
       </form> 
      </li> 

     </ul> 
    </fieldset> 
</div> 

的問題是,當一個網頁有URL

http://www.example.com/**english**/404.php?uri=somedata 

,我將提交$_POST的語言更改爲德語像:

http://www.example.com/**german**/404.php?uri=somedata 

例如$_GET['uri']將爲空,並且URL如下所示:

http://www.example.com/**german**/404.php?uri= 

啓動標頭函數之後。我試圖回顯該行,而不是標題,我將收到消息uri未定義。

Undefined variable: uri in /customers/7/4/1/example.com/httpd.www/scripts/footer_en.php on line 102 
Location: ../english/404.php?uri= 

奇怪有關的是,當我將調用頁面發送$_POST前看看該網站的源代碼中的變量$uri會正確地讀出$_GET參數,但後來它不工作在標題功能?

所以如果有人能告訴我該如何處理這個問題,那將會很好。我真的很感激。

非常感謝。

+0

爲什麼在布爾比較的兩個級別上任意停止?爲什麼不'if(((isset($ _ GET ['uri'])=== true)=== true)=== true)'? – deceze

+0

對不起,我沒有得到它。你能稍微解釋一下嗎? – bonny

+0

您不需要將'isset'的返回值嚴格地與一個布爾值進行比較,該布爾值將生成另一個布爾值,然後對其進行評估。 'isset'已經返回一個布爾值,只是'if(isset(...))'完全正確。只是編碼風格挑剔。 – deceze

回答

0

在您的表單中使用之前,將uri添加到您的表單操作中。

$form_action .= "?uri=" . $uri; 
+0

你好,謝謝你的回答。當添加它時,它會導致$ _SERVER ['PHP_SELF']與以前相同的錯誤。 SESSION/COOKIE上的更改生效,但由於$ _SERVER ['PHP_SELF'],語言目錄不會更改 – bonny