And*_*dre 9 php routes nginx url-routing
我的重定向过程显示了一些疯狂的东西.整个循环的第一部分工作正常(如果只输入第一个元素).
可能的网址如下:
www.site.com/category
www.site.com/category/product
但是也:
www.site.com/cart
使用site.com/jeans工作得很好.但是,当您点击某个产品时,会发生一些奇怪的事情.
categorie.php文件(用于显示类别)包括STILL,在此之后,包含product.php文件.
与购物车页面相同的故事(http://www.site.com/winkelwagen/).
所以我的包含在某些方面是错误的.Winkelwagen是我网站上的一个文件夹,它有一个索引文件.它应该包括http://www.site.com/winkelwagen/index.php而不是categorie.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");
}
}
?>
Run Code Online (Sandbox Code Playgroud)
我尝试删除了categorie.php文件,但它仍然显示出来(比如,地球上怎么样?!)
我很满意答案 - 我完全不知道我做错了什么.
也很高兴知道:当我在路由代码中注释掉include(categorie.php)部分时,该文件仍然包含在内......
Khe*_*hez 13
好的...欢迎来到Stack Overflow.我首先要说你被允许发布链接,试图通过使用"点"来破坏链接,实际上感觉更像是垃圾邮件,至少对我来说.
我将继续建议您不要将您的网站和该代码公开.它有各种安全漏洞,我不打算详细介绍.但是,我只想说我很好奇为什么你的用户被称为d284h1以及为什么你的网站/家庭在挂载点上/mnt/home/d284h1
...
听从我的话.您刚刚在一个非常公开的网站上发布了您的路由逻辑和您的网站.
关于你的代码.我真的希望这会破坏你的缩进而不是你的实际源代码.
你缺少一些控制逻辑.其中一些可能导致您遇到的文件包含.我还注意到了一个可能的错误,您正在测试并包含来自根目录的文件,而不是相对于您的站点路径.
更新:实际上回顾您的原始代码,绝对引用该文件$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");
}
Run Code Online (Sandbox Code Playgroud)
我的更新被评论过#
,这绝不是最终的形式.根据什么编码标准,看看PSR1是否有一个温和的想法.尽管最初感觉很麻烦,但它们旨在帮助您使您更加精通您对终极代码的追求.
我还会继续做的其他事情是:
!empty($var)
用isset($var[0])
,如果是$ var是一个字符串include($file);die;
用return include $file;
,如果你的主要范围是实际上关于#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();
Run Code Online (Sandbox Code Playgroud)
PS我没有解决您遇到的安全问题,因为我认为不应该使用您正在使用的代码.考虑使用框架或至少从一个框架中学习.路由是好MVC的基石,你走在正确的道路上,走得更远.