dongqiaolong9034 2015-06-05 09:21
浏览 26
已采纳

这是否可以使用pageswitch?

Usually i would use url parameters for a pageswitch,

lets say i got this switch so p is the page

if(isset($_GET['p']){
    $page = $_GET['p'];
    if(file_exists('includes/' . $page . '.php')){
        // include the page
    } else {
        // include error page
    }
} else {
    // include homepage
}

my url would look like this:

http://www.example.com?p=contact

is it possible to have a pageswitch with an url like this:

http://www.example.com/contact

mention the missing .php at the end of the url.

and can someone tell me if this php if else statement is a proper way to include files ?

  • 写回答

1条回答 默认 最新

  • dtu36380 2015-06-05 09:35
    关注

    Yes its possible to have a page switch with a page such as http://www.example.com/contact.

    You will need to re-write traffic however, so if you have access to a .htacess file you can add something like this

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^/(.+)/?$ http://www.example.com?p=$1 [NC,L,QSA]
    

    This way the user can navigate to /contact and it will re-direct to ?p=contact

    If you've got lots of pages may I suggest using a switch statement to check your $_GET?

    A switch statement in your case might look like this...

    if(isset($_GET['p'])){
       $page = $_GET['p'];
       switch($page){
    
         case 'contact':
           if(file_exists('includes/' . $page . '.php')){
              // include the page
          } else {
              // include error page
          }
          break; 
    
       }
    }else{
    
    }
    

    Hope this helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?