本文介绍了如何避免在PHP UNLINK安全隐患?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用UNLINK与 PHP AJAX 。我知道,这样是很危险的,因为每个人都可以删除任何文件。但我需要使用 AJAX ,因为我不能重新加载该页面时,我删除这些文件。

所以,我应该怎么做才能让删​​除的文件仅适用于谁拥有它的用户?

请让我知道其他的东西太多,如果你认为我在这里做什么错,还是别的什么你心目中,你认为这将是有益的:)

我的PHP code:


 < PHP

    $ photo_id = $ _GET ['photo_id'];
    $ thumbnail_id = $ _GET ['thumbnail_id'];

    功能deletePhotos($ ID){
        回到断开链接($ ID);
    }

    如果(使用isset($ photo_id)){
        deletePhotos($ photo_id);
    }
    如果(使用isset($ thumbnail_id)){
        deletePhotos($ thumbnail_id);
    }


 ?>
 

我的AJAX code:


 函数deletePhoto(照片,缩略图){

        变种照片= EN codeURIComponent(照片);
        VAR缩略图= EN codeURIComponent(缩略图);

        如果(window.XMLHtt prequest){// $ C $下IE7 +,火狐,Chrome,歌剧,Safari浏览器
          XMLHTTP =新XMLHtt prequest();
        }其他{// code对IE6,IE5
          XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
        }

        xmlhttp.onreadystatechange =功能(){
            如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200){
                的document.getElementById(媒介)的innerHTML = xmlhttp.responseText。
            }
        }
        xmlhttp.open(GET, "http://192.168.2.104/images/users/delete_photo.php?photo_id="+photos+"&thumbnail_id="+thumbnails,真正);
        xmlhttp.send();
    }
 

解决方案

您需要以某种方式验证用户的身份。

您的用户需要使用用户名和密码进行身份验证。

PHP会话可以用来记住,你应该使用一个数据库表或文本文件在服务器上存储文件的所有权信息。

然后,断开链接任何事情之前,你的逻辑应该确保目前认证的用户是文件的所有者。

I'm using UNLINK with PHP and AJAX. I know that in this way is very dangerous, because everyone can delete any files. But I need to use AJAX because I can't reload the page when I delete the files.

So how should I do to allow to delete the file only for the user who owns it?

Please let me know other things too if you think I'm doing here something wrong or something else what you have in mind and you think that it will be useful : )

My PHP code:


<?php

    $photo_id       = $_GET['photo_id'];
    $thumbnail_id   = $_GET['thumbnail_id'];

    function deletePhotos($id){
        return unlink($id);
    }

    if(isset($photo_id)){
        deletePhotos($photo_id);
    }
    if(isset($thumbnail_id)){
        deletePhotos($thumbnail_id);
    }


 ?>

My AJAX code:


function deletePhoto(photo, thumbnail){

        var photos = encodeURIComponent(photo);
        var thumbnails = encodeURIComponent(thumbnail);

        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
        } else {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                document.getElementById("media").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "http://192.168.2.104/images/users/delete_photo.php?photo_id="+photos+"&thumbnail_id="+thumbnails, true);
        xmlhttp.send();
    }
解决方案

You need to authenticate the user somehow.

Your user needs to be authenticated with a username and a password.

PHP session can be used to remember, and you should use a database table or a text file on the server to store file ownership information.

Then, before unlinking anything, your logic should make sure that the currently "authenticated" user is the owner of the file.

这篇关于如何避免在PHP UNLINK安全隐患?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 21:17