我的重定向过程显示了一些疯狂的东西。整个循环的第一部分运行良好(如果只输入第一个元素)。
可能的url如下:
www.site.com/category www.site.com/category/product
还:
www.site.com/cart
使用site.com/jeans工作很好。但是当你点击一个产品时,奇怪的事情发生了。
仍然包括category .php文件(用于显示类别),在该文件之后,还包括product.php文件。
购物车页面也是如此(http://www.site.com/winkelwagen/)。
所以我的include在某些点上是错误的。Winkelwagen是我网站上的一个文件夹,里面有一个索引文件。它应该包括http://www.site.com/winkelwagen/index.php,而不是category .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");
}
}
?>
我试着删除category .php文件,但它仍然显示(就像,怎么回事?!)
我对这个答案很兴奋——我完全不知道我做错了什么。
还很高兴知道:当我注释掉路由代码中的include(category .php)部分时,文件仍然包含…
Ok…欢迎来到Stack Overflow。我首先要说的是,你可以发布链接,试图通过使用"点"来破坏链接,实际上感觉更像垃圾邮件,至少对我来说。
我将继续建议您不要将您的站点和代码公开。它有各种安全漏洞,我不打算详细介绍。但是,我很好奇为什么你的用户名为d284h1以及为什么你的站点/主页位于挂载点/mnt/home/d284h1
…
注意我的话。您刚刚在very公共站点上发布了您的路由逻辑和站点。
关于代码。我真的希望这会破坏你的缩进,而不是破坏你的实际源代码。
你缺少一些控制逻辑。其中一些可能导致了您正在经历的文件包含。我还注意到一个可能的错误,您正在测试并包括来自根目录的文件,而不是相对于您的站点路径。
Update:
实际上回顾您的原始代码,绝对引用文件$file2 = "/$mult[0]/index.php";
导致categorie.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");
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,你会对什么是编码标准有一个大概的了解。它们的目的是帮助你,让你在追求终极代码的过程中更加熟练,尽管最初感觉很麻烦。
我将继续做的其他事情是:
- 将
!empty($var)
与isset($var[0])
交换,如果$var是字符串 - 交换
include($file);die;
与return include $file;
,如果你在主作用域 - 用三元操作符交换if/elseif块
实际上关于#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();
注:我没有解决你遇到的安全问题,因为我认为你正在使用的代码不应该被使用。考虑使用一个框架,或者至少从中学习。路由是好的MVC的基石,你走在正确的道路上,再往前走一步。
请您也测试一下并发送您的反馈,我只是重新构建了代码(我使用if elseif else使条件更严格)
<?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");
}
elseif(!empty($mult[0]) && empty($mult[1]))
{
$file = "$mult[0].php";
if($mult[0] == "index2")
{
include("index2.php");
die;
}
else{
// 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");
}
}
}
}
elseif(!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");
}
}
?>