2013-02-20 102 views
0

我需要一種方法來檢測'foreach'是否不成功。如果是失敗,則重複出現在當前'其他'中的相同錯誤消息測試如果一個Foreach成功

<?php 

    if(file_exists('redirects.xml')) { 
     $xml = simplexml_load_file('redirects.xml'); 
     if(isset($_GET['r']) && $_GET['r'] != '' && !is_array($_GET['r'])) { 
      foreach($xml->short as $shorts) { 
       if($shorts->name == $_GET['r']) { 
        header('Location: '.$shorts->url); 
        break; 
       } 
      } 
     } 
     else { 
      header("refresh:2;url=http://www.wlatw.co/"); 
      echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>'; 
     } 
    } 
?> 
+1

是什麼讓'foreach'環成功還是不成功?你的意思是說如果沒有'$ xml-> short'作爲非成功的迭代? – 2013-02-20 03:45:45

+2

你應該總是在'header('Location:...')'header後終止腳本。例如使用'die;'。 – 2013-02-20 03:46:05

回答

2

您需要添加一個變量來跟蹤循環的狀態。

<?php 

if(file_exists('redirects.xml')) { 
    $xml = simplexml_load_file('redirects.xml'); 
    if(isset($_GET['r']) && $_GET['r'] != '' && !is_array($_GET['r'])) { 
     $url_is_malformed = false; 
     foreach($xml->short as $shorts) { 
      if($shorts->name == $_GET['r']) { 
       header('Location: '.$shorts->url); 
       break; 
      } 
     } 
     $url_is_malformed = true; 
    } 
    else { 
     $file_doesnt_exist = true; 
    } 

    if($file_doesnt_exist || $url_is_malformed) 
    { 
     header("refresh:2;url=http://www.wlatw.co/"); 
     echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>'; 
    } 
} 

?>

+1

+1使代碼更優雅 – andho 2013-02-20 03:53:45

3

在開始循環之前創建一個標誌; 當它失敗時將其設置爲不成功。

<?php 

    if(file_exists('redirects.xml')) { 
     $xml = simplexml_load_file('redirects.xml'); 
     if(isset($_GET['r']) && $_GET['r'] != '' && !is_array($_GET['r'])) { 
      $success = false; // set the flag 
      foreach($xml->short as $shorts) { 
       if($shorts->name == $_GET['r']) { 
        header('Location: '.$shorts->url); 
        $success = true; 
        break; 
       } 
      } 

      if ($success) { // do what you want when not success ful. 
       header("refresh:2;url=http://www.wlatw.co/"); 
       echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>'; 
      } 
     } 
     else { 
      header("refresh:2;url=http://www.wlatw.co/"); 
      echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>'; 
     } 
    } 
?> 

但看你的代碼,你可以設置標題後,只是退出:

foreach($xml->short as $shorts) { 
    if($shorts->name == $_GET['r']) { 
     header('Location: '.$shorts->url); 
     exit; 
     break; 
    } 
} 

注:作爲@Sverri M.奧爾森說,你應該總是設置後停止腳本位置標題,無論是死亡,退出還是其他任何機制。

+1

啊,你打我吧。 – 2013-02-20 03:49:39

+0

我打算對退出進行評論,但如果代碼稍後沒有更改,那麼標誌將是最好的解決方案。 – 2013-02-20 05:15:04