2011-08-10 29 views
2

我們目前使用的開關箱URL配置,以幫助我們在我們的一些網址導航,林不知道,如果有一個更簡單的方法來做到這一點,但我不能似乎找到1PHP開關罩的URL

<?php if (! isset($_GET['step'])) 
    { 
     include('./step1.php'); 

    } else {  
     $page = $_GET['step']; 
     switch($page) 
     { 
      case '1': 
       include('./step1.php'); 
       break; 
      case '2': 
       include('./step2.php'); 
       break; 
     } 
    } 
    ?> 

現在這個系統的工作完美,但我們打的唯一的問題是,如果他們在xxxxxx.php類型?步驟= 3熱潮,他們只是得到了一個空白頁面,這應該是正確的,因爲不存在的情況下它處理「3」但我想知道的是..是否有任何PHP代碼我可以添加到底部,可能會告訴它除了那些2以外的任何情況下重定向回到xxxxx.php?

感謝

丹尼爾

回答

4

使用default情況。也就是說,你的開關切換到這樣的事情:

<?php if (! isset($_GET['step'])) 
    { 
     include('./step1.php'); 

    } else {  
     $page = $_GET['step']; 
     switch($page) 
     { 
      case '1': 
       include('./step1.php'); 
       break; 
      case '2': 
       include('./step2.php'); 
       break; 
      default: 
       // Default action 
      break; 
     } 
    } 
?> 

默認情況下將用於未明確規定每一種情況下被執行。

2

所有switch語句允許default情況下,如果沒有其他情況下,這是否會火。像...

switch ($foo) 
{ 
    case 1: 
    break; 
    ... 
    default: 
    header("Location: someOtherUrl"); 
} 

會工作。但是,您可能希望谷歌可以使用其他更強大和可擴展的頁面調度解決方案。

1

如何沿着線的東西不同的方法:

<?php 
$currentStep = $_GET['step']; 
$includePage = './step'.$currentStep.'.php'; # Assuming the pages are structured the same, i.e. stepN where N is a number 

if(!file_exists($includePage) || !isset($currentStep)){ # If file doesn't exist, then set the default page 
    $includePage = 'default.php'; # Should reflect the desired default page for steps not matching 1 or 2 
} 

include($includePage); 
?>