好的,这是我数据库中的2/5表,(它们之间没有关系):
USERS PAPERS
user_id p_id
username p_name
e-mail p_authors
password p_coauthor
p_jname
p_date
p_url
假设从PHP脚本填充。
注册后,会创建用户文件夹,然后用户可以将文件上传到他的文件夹。用户可以查看,访问和下载其他用户上传,我正在尝试阻止。
我不确定哪些脚本与c / p相关,所以生病尝试从脚本中显示代码的重要部分:
注册脚本:(脚本的其余部分只创建用户文件夹)
$uname = mysql_real_escape_string($_POST['uname']);
$email = mysql_real_escape_string($_POST['email']);
$upass = mysql_real_escape_string($_POST['pass']);
$sql = mysql_query ("SELECT username FROM users WHERE username='".$uname."'");
$num = mysql_num_rows($sql);
if ($num > 0) { ?>
<script>alert('Duplicate username');</script>
<?php } else {
if(mysql_query("INSERT INTO users(username,email,password) VALUES('$uname','$email','$upass')")) { ?>
<script>alert('Successfully registered');</script>
<?php }
登录脚本:
$email = mysql_real_escape_string($_POST['email']);
$upass = mysql_real_escape_string($_POST['pass']);
$sql = mysql_query("SELECT * FROM users WHERE email='".$email."'");
$num = mysql_fetch_assoc($sql);
if ($num['password'] == $upass) {
$_SESSION['user'] = $num['username'];
} else { ?>
<script>alert('wrong details');</script>
<?php }
发送文件和数据的和addfile脚本:
$sql = mysql_query("INSERT INTO papers (p_name, p_authors, p_cauthor, p_jname, p_date, p_url) VALUES ('$name', '$authors', '$cauthor', '$jname', '$date', '$filename')");
if($sql) { ?>
<script>alert('Data uploaded to database !');
window.location.href = "home.php";</script>
<?php } else { ?>
<script>alert('Data NOT uploaded to database !');
window.location.href = "home.php";</script>
<?php }
现在,当我在用户主页上执行此操作时:
$result = mysql_query("SELECT username, p_url FROM users, papers WHERE username='".$_SESSION['user']."'");
$output = mysql_fetch_array($result);
foreach ($output as $key => $val) {
$search = substr($val, 0, strpos($val, '.'));
$trim = str_replace($val, '', $search);
echo <<<HERE
<a href="http://localhost:8080/testfolder/users/{$_SESSION['user']}/{$val}" target="_blank">{$trim}</a><br />
HERE;
}
我可以从其他用户文件夹访问和下载所有数据和上传的文件。我如何防止这种情况,并使脚本只显示和下载数据库中记录的用户事物?我的用户和论文表构造是完全错误的,或者可能是上述查询中的事情?
答案 0 :(得分:0)
为防止发生这种情况,您 要在表格之间建立关系,并通过此方式将其关联起来。
您的查询正在检索users
中的每条记录,并将其映射到papers
您的表格必须有一个公共字段才能防止这种情况发生。我建议将users表中的user_id
添加到papers
表中并将您的查询重写为
SELECT
u.username,
p.p_url
FROM
users as u
INNER JOIN
papers as p
ON
p.user_id=u.user_id
WHERE
u.username='{$_SESSION['user']}'