2014-10-22 139 views
0

您好尊敬的程序员,404错误页面不工作的错误的动态网页

首先,我的网站最近完成,但程序员拒绝给予支持理由,这是合同的组成部分。但是,我只是想解决这个单一的问题,我可以管理这个网站,直到将来升级。

的网站页面有以下任何一种格式:

http://example.com/index.php?p=course&cid=xx 
http://example.com/index.php?service&sid=xx 
http://example.com/index.php?p=about-us 

我有一个404错误页面中的位置:

/include/404.php 

,它会显示为

http://example.com/index.php?p=404.php 

现在,我想设置的404错误页面时显示一个错误的页面说: http://example.com/index.php?p=about-us2.php

当输入一个错误的页面,这是会发生什么吧:

Warning: include(include/about-us2.php): failed to open stream: No such file or directory in /home/hanpan5/public_html/testing/index.php on line 127 

Warning: include(include/about-us2.php): failed to open stream: No such file or directory in /home/hanpan5/public_html/testing/index.php on line 127 

Warning: include(): Failed opening 'include/about-us2.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/hanpan5/public_html/testing/index.php on line 127 

我在前面加了404码但是,它影响到托管在http://blog.example.com 上的博客上的页面。当代码从.htaccess中删除时,博客页面再次正常工作。

我也注意到,有一些代码在index.php文件:

<div class="body"> 

     <?php include('include/menu.php'); ?> 

     <?php 
     if(!isset($_GET['p']) && @$_GET['p'] == '') 
     { 
     include('include/slider.php'); ?> 

     <?php include('include/services.php'); ?> 

     <?php include('include/strategy.php'); ?> 

     <?php include('include/testimonial.php'); 
     include('include/twitterfeeds.php'); 
     }else 
     { 
      $p = $_GET['p']; 
      include('include/'.$p.'.php'); 
     } 
     ?> 

     <?php include('include/footer.php'); ?> 

    </div> 

反正是有,我可以添加include('include/404.php');到,如果上述声明所述重定向帮助。或者我能做些什么来解决这个问题?

热切期待您的帮助。

非常感谢您提前。

/编辑1后** @ DaveG的第一个答案**/ 你好DaveG,这是我目前的执行你的解决方案:

<div class="body"> 

     <?php 

     include('include/menu.php'); 

     if(isset($_GET['p'])) 
      { 
       $p = $_GET['p']; 
       if(!file_exists('include/'.$p.'.php')) 
       { 
       header('HTTP/1.0 404 Not Found'); 
       include('include/404.php'); 
       exit(); 
       } 
      } 
     else 

     if(!isset($_GET['p']) && @$_GET['p'] == '') 
     { 
     include('include/slider.php'); ?> 

     <?php include('include/services.php'); ?> 

     <?php include('include/strategy.php'); ?> 

     <?php include('include/testimonial.php'); 
     include('include/twitterfeeds.php'); 
     } 
     else 
     if(isset($_GET['p'])) 
     { 
      $p = $_GET['p']; 
      include('include/'.$p.'.php'); 
     } 
     ?> 

     <?php include('include/footer.php'); ?> 

    </div> 

只有索引页(/index.php)加载完全。

但是,以例如/index.php?p=course&cid=13/index.php?p=abc结尾的文件未完全加载。仅加载菜单(menu.php)和页脚(footer.php)。

谢谢。

+0

你好@DaveG,请找我上面的编辑。非常感谢您的帮助。 – Ndianabasi 2014-10-22 16:21:11

+0

[编辑后]:问题是你把我的代码放在错误的地方。它应该放置在其他所有内容之前(所以在body-div之前,甚至在所有其他头部之前)以及'<?php'和'?>'之间。另外,'else'必须在我的代码后面被删除!我的代码是补充的,但其他代码也应该执行,所以在那里没有'else'! – DaveG 2014-10-23 06:32:41

+0

谢谢@DaveG。结果我会回到你身边。 – Ndianabasi 2014-10-23 06:46:03

回答

0

您可以与file_exists() -function在你的index.php PHP(http://php.net/manual/en/function.file-exists.php),之后,你可以返回404代码检查该文件的所有脑干:

<?php header('HTTP/1.0 404 Not Found'); ?> 

小心:这必须是完成第一个输出命令,所以首先检查存在,然后返回标题,然后继续其余的代码!

总数:

if(isset($_GET['p'])) 
{ 
    $p = $_GET['p']; 
    if(!file_exists('include/'.$p.'.php')) 
    { 
    header('HTTP/1.0 404 Not Found'); 
    include('include/404.php'); 
    exit(); 
    } 
} 
+0

你好@DaveG,请参阅我上面的修改。感谢您的帮助。 – Ndianabasi 2014-10-22 18:27:45

+0

谢谢@DaveG。我已将解决方案的实施添加到您的答案中。您可以同行评审,以便其他人看到它。非常感谢你,上帝保佑你。 – Ndianabasi 2014-10-23 07:11:28