我有一个简单的文件共享页面,允许用户上传图像并在列表中显示目录内容。我的数据库中有2个用户,我想制作它只有一个可以删除文件。我的数据库有一个用户类型列,这些值是1或2.这是我的代码:
<body>
<?php include "view/header.php" ?>
<div id="main">
<h2>Image to be uploaded</h2>
<form id="upload_form" action="." method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="upload">
<input type="file" name="file1"><br />
<input id="upload_button" type="submit" value="Upload">
</form>
<h2>Images in the directory</h2>
<?php if (count($files) == 0) : ?>
<p>No images have been uploaded.</p>
<?php else: ?>
<ul>
<?php foreach($files as $filename) :
$file_url= $image_dir . '/' . $filename;
$delete_url= '.?action=delete&filename=' . urlencode($filename);
?>
<li>
<a href= "<?php echo $delete_url;?>">
<img src= "images/delete.png" alt= "Delete"></a>
<a href= "<?php echo $file_url;?>">
<?php echo $filename;?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<div id="footerholder">
<div id="footer">
<?php include "view/footer.php" ?>
</div>
这是我的登录页面所指的:
<?php
$dsn= 'mysql:host=localhost; dbname=file_share';
$username= 'root';
$password= '';
try {
$db= new PDO($dsn, $username, $password); }
catch (PDOException $e) {
$error_message= $e->getMessage();
echo "<p>An error occured while connecting to the database: $error_message </p>"; }
//Only run code below if form has been posted
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if( isset($_POST['username'], $_POST['password'])
&& !empty($_POST['username']) && !empty($_POST['password']) ) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql= 'SELECT *
FROM `users`
WHERE `username` = :username
AND `password` = :password
LIMIT 1'; //SQL query with named placeholders
$stmt = $db->prepare($sql); //Returns a PDOStatement class object
$stmt->bindParam(':username',$username,PDO::PARAM_STR,16);
$stmt->bindParam(':password',$password,PDO::PARAM_STR,16);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$error = $stmt->errorInfo();//Debug
if($error[0] != '00000')
printf('SQL Error: <pre>%s</pre>', print_r($error, true));
if($stmt->rowCount() > 0) {
$_SESSION['loggedIn']= "true";
header("Location: uploader.php");
}
else {
echo 'Sorry username/password wrong';
echo '<br><a href="login.php">Try Again</a></br>';
}
答案 0 :(得分:0)
假设您的用户记录在$profile
:
<?php if($profile["type"] == 1){ ?>
<li>
<a href= "<?php echo $delete_url;?>">
<img src= "images/delete.png" alt= "Delete">
</a>
<a href= "<?php echo $file_url;?>">
<?php echo $filename;?>
</a>
</li>
<?php } ?>
您应该将显示删除链接的部分放在检查用户类型的语句中。
BTW,在您当前的代码中,如果黑客发现您的删除网址是... /?action = delete&amp; filename = FILE_NAME,他可以使用file_name简单地调用该网址。所以,你必须做两件事:
在链接上使用MD5并将其添加到链接的末尾。这个MD5可能是MD5(link + your_name +“hello”),这是不可能猜到的。你的链接就像:
href=".../action=delete&filename=FILE_NAME&hash=d131dd02c5e6eec4"
在服务器端,使用传递的哈希检查链接的md5。通过这样做,没有人可以操纵您的链接。
PS。我建议的只是一种保护网站安全的方法。