简体   繁体   中英

index.php not displaying. displays error page instead

i have a page with 4 links namely 1st.php, 2nd.php, 3rd.php, and 4th.php. my index.php displays the 3rd.php as default. i also created a 404.php (error page) so that when a user edits the URL it will not redirect to the index.php, instead it will display the error page. the problem is when i open my index.php page, it displays the error page instead. need help. this is my code:

 <?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');
  }
?>

Why do You add GET data to quarter if after that You re-initialize $quarter? And re-initialized $quarter stores incorrect filenames, if You have 1st.php not firstq2012.php. And 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'); }
If $_GET['quarter'] does not exists, which would be if opens just index.php not index.php?q=..., then it redirects.
I suggest that in Your switch...case You create variable $filename = '1st.php'; And then check if (in_array($quarter, $pages)) { include_once $filename; } else { ... } if (in_array($quarter, $pages)) { include_once $filename; } else { ... }

Your code can be cleaned up a lot.

Try using this intead:

<?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'];
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM