GET vs. POST Best Practices

后端 未结 10 1992
南方客
南方客 2020-11-27 19:44

For my web application (PHP/MYSQL), I show a list of items and a link on each row to delete the item. Right now, the link is



        
相关标签:
10条回答
  • 2020-11-27 20:06

    You should never change anything in your database (other than logging information or other ephemeral data) from a GET request. The issue is that there is various web spidering software, web accelerators, anti-virus programs, and the like, that will perform a GET request on every URL they find; you would not want them to delete items automatically when they do so. GET is also vulnerable to cross-site request forgery; if an attacker makes one of your users click on a link that performs a bad action (for instance, creating a tinyurl that redirects to a delete URL), then they can trick the user into using their permissions to delete something without realizing it.

    Yes, you will need a form that you submit to create a POST request. The other option is to use JavaScript and XMLHttpRequest, but that wont work for users who have JavaScript disabled.

    You should also ensure that once you have accepted the data from the POST request, instead of returning a new page in response to that request, you should redirect the user to a page accessed by a GET request. This way, they will not accidentally re-send the POST request if they hit reload, or hit their back button later in their browsing session.

    0 讨论(0)
  • 2020-11-27 20:08

    POST is not a protection from all malicious behavior as some people have implied. Malicious users can still create links (that contain javascript to do the POST) and cause the same cross-site scripting problems as with GET. (<a href="javascript:function () {...}"/>)

    That said, all the other reasons for using POST over GET apply (crawlers and the like).

    0 讨论(0)
  • 2020-11-27 20:10

    It’s still better to do it with GET.

    If your problem is that you don’t like the ugly URL of your link, you should be able to fix that with mod_rewrite (if you use Apache web server).

    Edit: There is no reason whatsoever to use POST here. No. Reason.

    People around here write about safe and unsafe as if chosing method can influence safety in any way. Of course you should authenticate your user no matter what method you choose. If you don’t, then your software is already broken. If you use javascript to emulate sending a form when you don’t have a form, and don’t need it, don’t need any javascript at all, then your software is already broken.

    Actually, about 90% of web software is broken, because people have no idea about what they are doing.

    Ignore this comments’ being heavily minused by some strange people. Avoid javascript (no need), avoid POST (no reason), authenticate your user (safety), make the href beautiful with mod_rewrite or some other way (being nice).

    0 讨论(0)
  • 2020-11-27 20:13

    You should not use an href to delete an item.

    I would suggest doing this the old fashioned way and implementing a form-post for each row/item.

    For example:

    <tr><td>Item 1</td><td><form action=/delete method=post><input type=hidden name=id value=1><input type=submit value=Delete></form></tr>
    <tr><td>Item 2</td><td><form action=/delete method=post><input type=hidden name=id value=2><input type=submit value=Delete></form></tr>
    <tr><td>Item 5</td><td><form action=/delete method=post><input type=hidden name=id value=5><input type=submit value=Delete></form></tr>
    

    Two other options: 1) using one with for each item 2) Ajax (but you will need to be proficient in Ajax)

    0 讨论(0)
  • 2020-11-27 20:23

    you can also use get but you would need to check session values to ensure that it's the owner of the post who's attempting to delete. get is not "universally unsafe". it totally depends on how you use it.

    0 讨论(0)
  • 2020-11-27 20:26

    Here's a good example of why not to use GET to change server state:

    http://www.infoworld.com/article/08/06/16/25FE-stupid-users-part-3-admins_5.html

    The key portion is:

    "It logged into the administrative area and followed the 'delete' link for every entry," the admin says.

    If the delete had been coded as a POST this never would have happened. (OTOH we'd be robbed of a funny sysadmin story.)

    0 讨论(0)
提交回复
热议问题