【问题标题】:PHP script dynamic display of files in directory based on user privileges?PHP脚本根据用户权限动态显示目录中的文件?
【发布时间】:2013-08-29 17:02:40
【问题描述】:

如果用户登录,我已经制作了以下脚本来显示目录中的文件:

<?php
session_start();
require_once("path/file.php");

if (!empty($_SESSION[username]))
{echo "You <b>$_SESSION[username]</b> are registered.";

$dirPath = dir('includes/path/');
$docArray = array();
while (($file = $dirPath->read()) !== false)
{
  if ((substr($file, -3)=="pdf") || (substr($file, -3)=="doc"))
  {
     $docArray[ ] = trim($file);
  }
}
$dirPath->close();
sort($docArray);
$c = count($docArray);
foreach($docArray as $filename)
{
    echo "<div><a href=\"./includes/path/$filename\">Download '$filename'</a></div>";
    echo "<br/>";
} 
 include('logout.php');
}

else
{echo "somethingsomething";

include('login.php');
}
?>

在成员表中有两列 MSV 和 LTP,可能的值为 0、1。此外,我还必须到目录 /path/LTP 和 /path/MSV。

我需要在脚本中添加一个内容,如果用户拥有 LTP 或/和 MSV 的权限,文件将相应地显示。

【问题讨论】:

  • 您没有显示任何处理您的成员表的代码(我假设它是一个 MySQL 数据库),我假设 path/MSV / path/LTP 部分将是 $dirPath?请澄清。
  • 是的,所有文件都存储在mysql数据库的成员表中。手动插入用户 username|password|company|LTP|MSV。
  • 文件路径目前是/includes/published/ 这应该分成两个目录/includes/MSV和/includes/LTP/

标签: php mysql validation path directory


【解决方案1】:

您需要将字段LTPMSV 的值存储到$_SESSION 中,并在您用来读取这些文件夹的php 文件中尝试这样的操作

读取 LTP 文件夹

if(!empty($_SESSION[LTP])){
 //code to read LTP folder
}else{
//Cannot read LTP folder 
}

读取 MSV 文件夹

if(!empty($_SESSION[MSV])){
//code to read MSV folder
}else{
//Cannot read MSV folder
}

假设0 是拒绝访问,1 是授予读取权限

【讨论】:

  • 这些值存储在 mysql 数据库中。所有成员都是手动插入的用户名\
  • @user1185197 你知道如何从 mysql 中获取这些值并将它们存储到 $_SESSION 中???
【解决方案2】:

为用户查询数据库,并在获取行后构建逻辑。

$username = mysqli_real_escape_string($con, $_SESSION['username']);
$query = "SELECT `LTP`, `MSV` FROM `members` WHERE `username`='".$username."'";
$data = mysqli_query($con, $query) or die(mysqli_error($con));
$row = mysqli_fetch_array($data);

if ($row['MSV'] == '1') {
    //provide access to MSV files here.
}

if ($row['LTP'] == '1') {
    //provide access to LTP files here.
}

请注意,die 语句用于调试,对于生产用途来说不够优雅。

【讨论】:

  • 谢谢一百万。我会试一试的。
猜你喜欢
  • 2013-02-15
  • 2011-09-07
  • 2017-10-04
  • 2013-07-30
  • 2011-01-06
  • 1970-01-01
  • 2019-09-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多