有没有办法简化这个案例陈述?

发布于 2024-11-25 19:53:29 字数 492 浏览 0 评论 0原文

我有这个 PHP case 语句,

switch ($parts[count($parts) - 1]) {
    case 'restaurant_pos':
        include($_SERVER['DOCUMENT_ROOT'] . '/pages/restaurant_pos.php');
        break;
    case 'retail_pos':
    include($_SERVER['DOCUMENT_ROOT'] . '/pages/retail_pos.php');
        break;  
    .....

}

效果很好,但是我有很多很多文件(比如 190),我很想知道是否有一种方法可以使这个 case 语句适用于任何东西,这样我就不必执行 190 个 case 条件。我想我可以在这种情况下使用条件,也许看看该文件是否存在,如果存在则显示,如果不存在则可能是 404 页面,但我不确定执行此操作的好方法...任何想法都会有帮助很多

I have this PHP case statement

switch ($parts[count($parts) - 1]) {
    case 'restaurant_pos':
        include($_SERVER['DOCUMENT_ROOT'] . '/pages/restaurant_pos.php');
        break;
    case 'retail_pos':
    include($_SERVER['DOCUMENT_ROOT'] . '/pages/retail_pos.php');
        break;  
    .....

}

Which works great but I have many many files (like 190) and I would love to know if there is a way to make this case statement many work with anything so I dont have to do 190 case conditions. I was thinking I can use the condtion in the case and maybe see if that file is present and if so then display and if not then maybe a 404 page but i was not sure a good way to do this...any ideas would help alot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

动次打次papapa 2024-12-02 19:53:29

您可以在数组中预定义文件名,然后使用 in_array 来检查名称是否存在:

$files = array('restaurant_pos', 'retail_pos', ......);
$file = $parts[count($parts) - 1];
if (in_array($file, $files)) {
    include($_SERVER['DOCUMENT_ROOT'] . "/pages/$file.php");
}

You can predefine file names in an array and then use in_array in order to check name's existence:

$files = array('restaurant_pos', 'retail_pos', ......);
$file = $parts[count($parts) - 1];
if (in_array($file, $files)) {
    include($_SERVER['DOCUMENT_ROOT'] . "/pages/$file.php");
}
听不够的曲调 2024-12-02 19:53:29

如果不是用户输入,你可以这样做

$include = $parts[count($parts) - 1];
if ($include) {
    if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/pages/'.$include.'.php')){
          include $_SERVER['DOCUMENT_ROOT'] . '/pages/'.$include.'.php';
    }
}

重复,如果 $include 是从用户输入填充的,则不要这样做!

If it's not user input, you can do it like

$include = $parts[count($parts) - 1];
if ($include) {
    if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/pages/'.$include.'.php')){
          include $_SERVER['DOCUMENT_ROOT'] . '/pages/'.$include.'.php';
    }
}

repeating, don't do this if $include is being filled from user's input !

颜漓半夏 2024-12-02 19:53:29

这是一个没有安全检查的简单实现:

$file=$_SERVER['DOCUMENT_ROOT']."/pages/".$parts[count($parts) - 1].".php";
if(file_exists($file)) include $file;
else show404();

例如,为了使其更安全,您可以从 $parts[count($parts) - 1] 中删除斜杠

This is a simple implementation without security checks:

$file=$_SERVER['DOCUMENT_ROOT']."/pages/".$parts[count($parts) - 1].".php";
if(file_exists($file)) include $file;
else show404();

To make it more secure for example you can remove slashes from $parts[count($parts) - 1]

柳若烟 2024-12-02 19:53:29

检查该文件是否存在,然后包含它。

请注意,您必须验证 $page 的内容,以确保它不包含类似 /../../../../ 的路径来尝试如果这是用户输入,则读取文件系统上的其他位置。

例如,如果您知道所有路径都是带有下划线的字母数字,您可以这样做:

$page = $parts[count($parts)] - 1;

if (preg_match('/^[A-Z0-9_]+$/i', $page)) {
  // it's okay, so include it.
  if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/pages/$page.php") {
    include($_SERVER['DOCUMENT_ROOT'] . "/pages/$page.php");
  }
}

Check that the file exists, and then include it.

Note that you MUST validate the contents of $page to be sure it doesn't include a path like /../../../../ to attempt to read somewhere else on your filesystem if this is to be user input.

If you know, for example that all your paths will be alphanumeric with underscores, you could do:

$page = $parts[count($parts)] - 1;

if (preg_match('/^[A-Z0-9_]+$/i', $page)) {
  // it's okay, so include it.
  if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/pages/$page.php") {
    include($_SERVER['DOCUMENT_ROOT'] . "/pages/$page.php");
  }
}
堇色安年 2024-12-02 19:53:29

为什么不做这样的事情呢?

$include_file = $_SERVER['DOCUMENT_ROOT'] . '/pages/' . $parts[count($parts) - 1] . '.php';

if (file_exists( $include_file ))
{
    include( $include_file );
}

Why not something like this?

$include_file = $_SERVER['DOCUMENT_ROOT'] . '/pages/' . $parts[count($parts) - 1] . '.php';

if (file_exists( $include_file ))
{
    include( $include_file );
}
一抹苦笑 2024-12-02 19:53:29
if (file_exists($path = $_SERVER['DOCUMENT_ROOT'].'/pages/'.$parts[count($parts) - 1].'.php')
{
    include $path;
}
if (file_exists($path = $_SERVER['DOCUMENT_ROOT'].'/pages/'.$parts[count($parts) - 1].'.php')
{
    include $path;
}
谜泪 2024-12-02 19:53:29

另一种方法是检查给定文件是否确实存在于特定目录中:

$file = $_SERVER['DOCUMENT_ROOT'] . '/' . basename($parts[count($parts) - 1]) . '.php';
if (is_file($file)) include($file);

Another approach would be to check if the given file really exists in a particular directory:

$file = $_SERVER['DOCUMENT_ROOT'] . '/' . basename($parts[count($parts) - 1]) . '.php';
if (is_file($file)) include($file);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文