get_magic_quotes() when inserting data using MYSQL improved.

Discussion in 'PHP' started by Mitchell, Jun 10, 2010.

  1. #1
    On the server I am renting, get_magic_quotes() is turned on. This means back slashes are automatically added. One of three books I am reading doesn’t go into detail about this, but says get_magic_quotes() is being discontinued. Assuming I cannot turn off get_magic_quotes(), It says use nuke_magic_quotes() to remove these added back slashes put there by get_magic_quotes(). And if I am correct in my understanding, I must use prepared statements to filter the text input to MYSQLI, I standing for improved, instead of using addslashes().

    1. Do I Have this correct. Do I use nuke_magic_quotes() and prepared statements and don't use addslashes()?

    2. Is this just for general text and not email or numbers?

    Thanks for advice.
     
    Last edited: Jun 10, 2010
    Mitchell, Jun 10, 2010 IP
  2. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Add this to get these backshlashes stripped
    
    if (get_magic_quotes_gpc()) {
        $lastname = stripslashes($_POST['lastname']);
    }
    
    PHP:
    Dont miss to escape that value before writing it to the database. (beware of mysql injection)

    for MYSQL
    
      $lastname = mysql_real_escape_string($lastname);
      $sql = "INSERT INTO lastnames (lastname) VALUES ('$lastname')";
    
    PHP:
    for MYSQLi
    
      $lastname = mysqli_real_escape_string($lastname);
      $sql = "INSERT INTO lastnames (lastname) VALUES ('$lastname')";
    
    PHP:
    Since PHP is typeless you should check if a number is really a number "function.is-numeric.php", in doubt escape it ^^

    Credits: function.get-magic-quotes-gpc.php
     
    Last edited: Jun 11, 2010
    flexdex, Jun 11, 2010 IP
  3. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #3


    Have a Look in the PHP Site http://ca.php.net/manual/en/function.get-magic-quotes-gpc.php
     
    roopajyothi, Jun 11, 2010 IP
  4. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Follow roopajyothi's hint, she is very clever.
     
    flexdex, Jun 11, 2010 IP
  5. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for your replies.
     
    Mitchell, Jun 11, 2010 IP
  6. eamiro

    eamiro Well-Known Member

    Messages:
    274
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #6
    mysql_real_escape_string(stripslashes($var)); 
    PHP:
    I think this works in all situations.
     
    eamiro, Jun 14, 2010 IP