簡體   English   中英

index.php不顯示。 顯示錯誤頁面

[英]index.php not displaying. displays error page instead

我有一個包含4個鏈接的頁面,分別是1st.php,2nd.php,3rd.php和4th.php。 我的index.php默認顯示3rd.php。 我還創建了一個404.php(錯誤頁面),以便當用戶編輯URL時,它不會重定向到index.php,而是顯示錯誤頁面。 問題是當我打開index.php頁面時,它顯示錯誤頁面。 需要幫忙。 這是我的代碼:

 <?php 

  $quarters = array('Q1', 'Q2', 'Q3', 'Q4');
        $quarter = 'Q3';

        if(isset($_GET['quarter']) && in_array($_GET['quarter'], $quarters)) {
          $quarter = $_GET['quarter'];  
        }

        switch($quarter) {
          case 'Q1' :
            $quarter = 'firstq2012.php';
          break;
          case 'Q2' :
            $quarter = 'secondq2012.php';
          break;
          case 'Q3' :
            $quarter = 'thirdq2012.php';
          break;
          case 'Q4' :
            $quarter = 'fourthq2012.php';
          break;
        }

    $pages = array('Q1','Q2','Q3','Q4');
    if (in_array($_GET['quarter'], $pages)){            
                    include_once $quarter;  
 }
 else {
 header('Location: 404.php');
  }
?>

如果之后重新初始化$ quarter,為什么還要在季度中添加GET數據? 如果您擁有1st.php而不是firstq2012.php,則重新初始化的$ quarter將存儲不正確的文件名。 並且if (in_array($_GET['quarter'], $pages)){
include_once $quarter;
} else { header('Location: 404.php'); }
if (in_array($_GET['quarter'], $pages)){
include_once $quarter;
} else { header('Location: 404.php'); }
if (in_array($_GET['quarter'], $pages)){
include_once $quarter;
} else { header('Location: 404.php'); }
如果$ _GET ['quarter']不存在,那就是如果只打開index.php而不打開index.php?q = ...,那么它將重定向。
我建議在您的switch ...案例中,您創建變量$ filename ='1st.php'; 然后檢查if (in_array($quarter, $pages)) { include_once $filename; } else { ... } if (in_array($quarter, $pages)) { include_once $filename; } else { ... }

您的代碼可以清理很多。

嘗試使用此界面:

<?php 
$quarters = array('Q1' => 'firstq2012.php', 'Q2' => 'secondq2012.php', 'Q3' => 'thirdq2012.php', 'Q4' => 'fourthq2012.php');
if(isset($_GET['quarter'])) {
    if(in_array($_GET['quarter'], array_keys($quarters))) {
        include_once $quarters[$_GET['quarter']];
    } else {
        header('Location: 404.php');
    }
} else {
    include_once $quarters['Q3'];
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM