Avoiding sql injection in php

Discussion in 'PHP' started by CuBz, Jul 30, 2009.

  1. #1
    I have a text based mafia website and it is open to sql injections.

    If someone types the following in a text field:

    ',money='1000000
    Code (markup):
    it will set ther money to £1,000,000

    What do i put on the php code to prvent this from being used?

    or do i have to change something in PHPMyAdmin

    and is there a way to do it so i do not have to edit every single page? like i have a functions page that is included on every page, is there something i can do on this one page to save me the time of doing it on every page?

    Please help beause this is causing many problems, i have had to cose my website.

    Thanks
     
    CuBz, Jul 30, 2009 IP
  2. ptalent

    ptalent Peon

    Messages:
    26
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    addslashes($_POST['fieldname']);
     
    ptalent, Jul 30, 2009 IP
  3. Martinoes

    Martinoes Peon

    Messages:
    110
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Martinoes, Jul 30, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    As martinoes has said, don't just use addslashes(). That does not work for advanced attacks. Use mysqli_real_escape_string(). Better yet would be for you to start using something like the PDO extension (which requires PHP5.1).
     
    premiumscripts, Aug 3, 2009 IP
  5. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #5
    Why not validating your input? when only requireing numbers you won't allow other symbols etc.

    is_numeric
    is_float
    is_double

    and strings you always need to use addslashes/mysql_real(_escape)_string

    Newer version
     
    EricBruggema, Aug 5, 2009 IP
  6. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #6
    No need to use a function^, just put (int) before it.
    $var = (int) $_POST['value'];
    Code (markup):
     
    Sky AK47, Aug 5, 2009 IP
  7. olddocks

    olddocks Notable Member

    Messages:
    3,275
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    215
  8. niceboy

    niceboy Well-Known Member

    Messages:
    1,170
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    128
    #8
    niceboy, Aug 5, 2009 IP