php – 包含文件不起作用 – url路由
我的重定向过程显示出一些疯狂的东西.整个循环的第一部分工作正常(如果只输入第一个元素).
可能的url的样子如下:
但也:
使用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文件,但它仍然显示(像,如何在地球上?!) 我很高兴得到答案 – 我完全不知道我在做错什么 也很高兴知道:当我注释掉路由代码中的include(categories.php)部分时,文件是STILL …
好的…欢迎来到Stack Overflow.我会开始说你被允许发布链接,试图通过使用“点”实际上更像垃圾邮件来破坏链接,至少对我来说.
我会继续建议您不要与您的网站和该代码公开.它有各种安全漏洞,我不会详细介绍.但是,我们只是说我很好奇为什么你的用户被称为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) 实际上关于#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的角落石头,你在正确的道路上,走一步. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |