【问题标题】:PHP strpos function conflicts with "../" file directory system [duplicate]PHP strpos函数与“../”文件目录系统冲突[重复]
【发布时间】:2015-10-19 19:48:33
【问题描述】:

我已经创建了一个文件目录系统。这里有一个创建目录的功能,我想阻止用户在 ../ 中创建目录,即一个文件夹,因此我创建了一个带有 strpos 的 if 语句来搜索它。代码如下:

<div class="FormElement">
  <form method="post">
    <input type="text" name="newFolder" id="newFolder" class="newFolder"/>
    <input type="submit" value="Create">
  </form>

  <?php
    $uniqueUserPath = $_SESSION['userPath'];
    $folderName = $_POST['newFolder'];
    $makeFolder = $uniqueUserPath . "/" . $folderName;
    // mkdir($uniqueUserPath . "/" . $folderName);

    if (strpos($folderName, "../") == true) {
      echo 'there is a slash.';
    } else {
      mkdir($uniqueUserPath . "/" . $folderName);
      echo 'there isnt a slash';
    }
  ?>
</div>

如果你在那里输入“../”,它仍然会回显,没有斜线,更重要的是,它会开始在用户文件夹之外的文件夹中创建目录。

任何帮助将不胜感激 亲切的问候,

【问题讨论】:

  • 利用realpath
  • 你能给我一个例子吗,因为我在阅读 php 文档后不知道如何使用它:)
  • === true 因为0 == true 但你想要!== false
  • strpos 不会返回布尔值,除非它找不到针。所以!== false 就是你想要的。

标签: php mysql post directory


【解决方案1】:

strpos($folderName, "../") == true 必须是strpos($folderName, "../") !== false

原因是如果找到匹配项,它会返回匹配项的字符索引(例如 5),然后将其评估为 true,因为 5 == true 为 true。

如果没有匹配,它返回布尔值false,所以你应该寻找它。

【讨论】:

  • 如果我想阻止用户输入 我该怎么做?或者至少要转义字符串,当然没有mysql函数。
  • 查看strip_tags。它还会去除 PHP 标签。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
相关资源
最近更新 更多