可能的url的样子如下:
www.site.com/category
www.site.com/category/product
但也:
www.site.com/cart
使用site.com/jeans工作很好.但是当您点击产品时,会发生奇怪的事情.
category.PHP文件(用于显示类别)是包含STILL,之后包含product.PHP文件.
与购物车页面(http://www.site.com/winkelwagen/)相同的故事.
所以我的包括在某些时候是错的. Winkelwagen是我网站上有一个索引文件的文件夹.它应该包括http://www.site.com/winkelwagen/index.php,而不是categories.PHP.
路线代码:
<?PHP $mult = Array(); if( ! empty( $_SERVER[ 'REQUEST_URI' ] ) ) { $mult = explode ( '/',substr ( $_SERVER[ 'REQUEST_URI' ],1 ) ); } else if( ! empty( $_SERVER[ 'ORIG_PATH_INFO' ] ) ) { $mult = explode ( '/',substr ( $_SERVER[ 'ORIG_PATH_INFO' ],1 ) ); } else if( ! empty( $_SERVER[ 'PATH_INFO' ] ) ) { $mult = explode ( '/',substr ( $_SERVER[ 'PATH_INFO' ],1 ) ); } if(empty($mult[0])) { include("comingsoon/index.html"); } if(!empty($mult[0]) && empty($mult[1])) { $file = "$mult[0].PHP"; if($mult[0] == "index2") { include("index2.PHP"); die; } // if file exists include file if(file_exists($file)) { include($file); } else { $file2 = "/$mult[0]/index.PHP"; // if folder index file exists include that file if(file_exists($file2)) { include($file2); } else { // if folder index file doesn't exist,send to category page $_GET['q'] = $mult[0]; include("categorie.PHP"); } } } if(!empty($mult[0]) && !empty($mult[1])) { if($mult[0] == "add") { $_GET['addid'] = $mult[1]; include("addtocart.PHP"); } elseif($mult[0] == "remove") { $_GET['removeid'] = $mult[1]; include("deletefromcart.PHP"); } // check if folder exists (first part of the url) elseif(is_dir($mult[0])) { // check if file .PHP (second part of the url) exists $filenew = "$mult[0]/$mult[1].PHP"; if(file_exists($filenew)) { // include that file include("$mult[0]/$mult[1].PHP"); } else { // second file does not exist,do something } } else { // folder does not exist so redirect to product page $_GET['c'] = $mult[0]; $_GET['p'] = $mult[1]; include("product.PHP"); } } ?>
我尝试删除categories.PHP文件,但它仍然显示(像,如何在地球上?!)
我很高兴得到答案 – 我完全不知道我在做错什么
我会继续建议您不要与您的网站和该代码公开.它有各种安全漏洞,我不会详细介绍.但是,我们只是说我很好奇为什么你的用户被称为d284h1,为什么你的站点/ home在一个挂载点/ mnt / home / d284h1 …
关于你的代码我真的希望这是摧毁你的缩进,而不是你的实际源代码.
你缺少一些控制逻辑.其中一些可能导致您遇到的文件夹.我也注意到一个可能的错误,在那里您正在测试,并从根目录中包含一个文件,而不是相对于您的站点路径.
更新:实际上回看你的原始代码,绝对参考文件$file2 =“/$mult[0]/index.PHP”;正在导致分类.PHP加载.没有正确的控制逻辑,导致文件中引起多个内含物.
自由地修改你的代码,温和地.以下代码,不应该继续包含任何随机文件.除非包含的文件本身就这样做.
$mult = array(); if( ! empty( $_SERVER[ 'REQUEST_URI' ] ) ) { $mult = explode ( '/',1 ) ); } else if( ! empty( $_SERVER[ 'ORIG_PATH_INFO' ] ) ) { $mult = explode ( '/',1 ) ); } else if( ! empty( $_SERVER[ 'PATH_INFO' ] ) ) { $mult = explode ( '/',1 ) ); } if (empty($mult[0])) { include("comingsoon/index.html"); die; #missing } # no need to test for !empty($mult[0]),if it were empty,the above die would fire if (empty($mult[1])) { $file = "$mult[0].PHP"; if($mult[0] == "index2") { include("index2.PHP"); die; } // if file exists include file if (file_exists($file)) { include($file); die; # missing die } # no need for else,you just die'd # renamed $file2 to $file,don't use temporary variable names in global scope. It clutters your application $file = "$mult[0]/index.PHP";# are you sure you meant to include from the root level? // if folder index file exists include that file if (file_exists($file)) { include($file); die;# missing die } # no need for else,you just die'd // if folder index file doesn't exist,send to category page $_GET['q'] = $mult[0]; include("categorie.PHP"); die;# missing die } # don't do succesive if/elseif on the same variable,use a switch! switch($mult[0]) { case'add': $_GET['addid'] = $mult[1]; include('addtocart.PHP'); break; case'remove': $_GET['removeid'] = $mult[1]; include('deletefromcart.PHP'); break; } if (is_dir($mult[0])) { // check if file .PHP (second part of the url) exists $filenew = "$mult[0]/$mult[1].PHP"; if(file_exists($filenew)) { // include that file include("$mult[0]/$mult[1].PHP"); die; # missing die } } else { // folder does not exist so redirect to product page $_GET['c'] = $mult[0]; $_GET['p'] = $mult[1]; include("product.PHP"); }
我的更新用#注释,这绝对不是最终的形式.看看PSR1的一个温和的想法,coding standards是什么.它们旨在帮助您,使您更精通您对最终代码的追求,尽管最初感觉很麻烦.
我继续做的其他事情是:
如果$var是一个字符串,则使用isset($var [0])交换空($var)
>交换包含($file); die;返回包含$file;如果你在主要范围内
>如果/ elseif块与ternary operators交换
实际上关于#3,这里有一个例子:
$mult = isset($_SERVER['REQUEST_URI'][0]) ? $_SERVER['REQUEST_URI'] : isset($_SERVER['ORIG_PATH_INFO'][0]) ? $_SERVER['ORIG_PATH_INFO'] : isset($_SERVER['PATH_INFO'][0]) ? $_SERVER['PATH_INFO'] : false ; $mult = $mult ? explode('/',substr($mult,1)) : array();
附:我没有修复您所拥有的安全问题,因为我相信您使用的代码不应该被使用.考虑using a framework或至少learning from one.路线是好的MVC的角落石头,你在正确的道路上,走一步.