1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

issues with anti sql injection code...

Discussion in 'PHP' started by Shimurai, Jul 24, 2011.

  1. #1
    Hi guys,

    I'm using this code for anti sql injection:
    
    foreach ($_GET as $k => $value)
    {
        $_GET[$k] = ereg_replace("[^A-Za-z0-9\,\ \?\_\@\.\]", "",$value);
    
    }
    foreach($_POST as $v => $value)
    {
        $_POST[$v] = ereg_replace("[^A-Za-z0-9\,\ \?\_\@\.\]", "",$value);
    }
    
    foreach($_COOKIE as $Y => $value)
    {
        $_COOKIE[$Y] = ereg_replace("[^A-Za-z0-9\,\ \?\_\@\.\]", "",$value);
    }
    
    PHP:
    and now I'm having some issues adding data to my database because it will remove all the slashes and what I'm trying to add to the database is an url ..

    So i have a form in post method with a text input where i want to copy an url there

    example: ( $_POST['url'] being http://google.com/ )
    $somevar = $_POST['url'];
    PHP:
    the anti sql code is making the url like this:
    httpgoogle.com
    PHP:
    how can I fix this?
     
    Shimurai, Jul 24, 2011 IP
  2. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #2
    This could be good for you to use instead? Or maybe change the / instead the html code of it. "/" then change it back when your ready to display the code?

    function makesafe($varvalue)
      {
         
          if (empty($varvalue))
          { $varvalue = null; }
          else
          {
              $varvalue = htmlentities($varvalue, ENT_QUOTES);
              $varvalue = strip_tags($varvalue);
              $varvalue = stripslashes($varvalue);
              $varvalue = str_remove($varvalue,
                          array('SELECT',
                                'UNION',
                                'UPDATE',
                                'DELETE',
                                'WHERE',
                                '\r')
                          );
              $varvalue = trim($varvalue);
          }
          return $varvalue;
      }
    
    function clean($str)
      {
         $str = strip_tags($str);
         $str = ereg_replace('[^a-zA-Z ]', ' ', $str);
         $str = eregi_replace(' +', ' ', $str);
         $str = trim($str);
         $str = strtolower($str);
         return $str;  
      }
    
    function clean2($str) {
        $str = strip_tags($str);
        $str = preg_replace('/([^a-z ]+)/i', ' ', $str);
        $str = preg_replace('/([ ]+)/', ' ', $str);
        $str = strtolower($str);
        return $str;  
    }
    
    PHP:
     
    exodus, Jul 24, 2011 IP
  3. bogi

    bogi Well-Known Member

    Messages:
    482
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    140
    #3
    bogi, Jul 24, 2011 IP
    victa likes this.
  4. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #4
    indeed, use mysql_real_escape_string and use your regexes for validating input. So if you want to validate a link use php.net/filter_var with FILTER_VALIDATE_URL

    :)
     
    EricBruggema, Jul 25, 2011 IP
  5. ZeroGamma

    ZeroGamma Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Do not use regular expressions. This is not good practice.

    Use the mysql_real_escape_string or mysqli->escape_string functions.
     
    ZeroGamma, Jul 28, 2011 IP
  6. Shimurai

    Shimurai Well-Known Member

    Messages:
    186
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #6
    so by using mysql_real_escape_string at my sql queries im totally preventing sql injections ?

    thank you.
     
    Shimurai, Aug 12, 2011 IP