【问题标题】:How to check which URL is loaded and include a page based on this?如何检查加载了哪个 URL 并包含基于此的页面?
【发布时间】:2011-10-29 00:16:20
【问题描述】:
<?php 
if(...) {
 include("home.php");
} else if(...) {
 include("about.php"); 
}
?>

如果我点击主页按钮,它将包含 home.php 页面,如果我点击 about 按钮,它将在页面加载时包含 about.php。我想这样,这样我就可以有一个 index.php 文件,而不是每个页面的多个文件。

有没有办法检查正在加载的 url?

【问题讨论】:

    标签: php html url ssi


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      您还可以使用 $_GET 和 switch 语句。首先查看get var是否设置,如果设置使用switch语句,否则使用默认。

      类似

      <?php 
         if(isset($_GET['page'])) { 
      $page = $_GET['page'];
       // roll GET into a var so you can do some error checking if you wish, // 
       // clean it up if need be //
          switch($page) {
              case 'about':
                  include('about.php');
                  break;
              case 'home':
                  include('contact.php');
                  break;
              default:
                  include('home.php');
                  break;
            } 
      } else {
        include('home.php');
      }
      ?>
      

      这样,您就有了双重保障。如果未设置 get,它会自动包含 home.php,但如果已设置,您有几个预定义的选项可供选择。如果有人弄乱了 url,无论如何都会包含 home.php。

      PHP switch statement

      【讨论】:

        【解决方案3】:

        您可能还想调查 mod_rewrite。

        【讨论】:

          猜你喜欢
          • 2021-10-05
          • 2016-04-16
          • 1970-01-01
          • 2011-10-21
          • 1970-01-01
          • 1970-01-01
          • 2012-10-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多