doulangpeng3933 2011-06-01 00:37
浏览 25
已采纳

php安全评论逻辑?

Ok, this might be obvious but its not clicking quite yet. I am creating a forum/blog esque app.

I grab the posts from the database rather securely but commenting is beginning to be a little more difficult. (I could just be paranoid, right?).

How do I add a comment without exposing the id of the parent message? (like in a hidden form field or query string, or something).

I guess I am a bit paranoid that someone might go into the code with firebug or something and change the hidden form field value to something else before submitting. I guess I would have to make sure the user has permission to comment to that particular post/category?

Things to note : The user is already logged in. Its not a public post

  • 写回答

4条回答 默认 最新

  • duanhuilao0787 2011-06-01 01:00
    关注

    I would recommend that you setup your database like so:

    Comments
    ---------
    id
    encodedID
    authorID
    parentID
    message
    

    Then, for the form field have two hidden values, one will be the encodedID, and the second will be a hash that you make. I would recommend the hash to be:

    <?php
    
    $hash = sha1(md5($encodedID . $userID . $_SERVER['REMOTE_ADDR'] . "abc1234"));
    
    ?>
    

    Then, when the user submits the form, validate that the hash is valid for the specific encodedID and user. Here is a brief code write up:

    <?php
    
    if(isset($_POST['submit']))
    {
        //Get the variables and all and sanitize the input of 'message'
        if(sha1(md5($_POST['value1']. $userID . $_SERVER['REMOTE_ADDR'] . "abc1234")) == $_POST['value2'])
        {
            //User is valid.
        }
        else
        {
            //Invalid user.
            //Document this.
        }
    }
    
    $value1 = $encodedID; //Grab this from your database
    $value2 = sha1(md5($value1 . $userID . $_SERVER['REMOTE_ADDR'] . "abc1234"));
    ?>
    
    <form method="post" action="comment.php">
    <input type="text" name="message" />
    <input type="hidden" name="value1" value="<?php echo $value1; ?>" />
    <input type="hidden" name="value2" value="<?php echo $value2; ?>" />
    <input type="submit" name="submit" value="Comment" />
    </form>
    

    Edit: Just a small tip, but I would recommend that you change value1 and value2 to something abstract, don't call it encodedID or anything like that, just so that it confuses any users that will attempt to try and break it.

    And yes md5 and sha1 are not completely secure, but for this case it will work since you want to be able to process the comments fast and efficiently.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图