The addslashes() function

Discussion in 'PHP' started by oo7ml, Jun 23, 2007.

  1. #1
    If i use the addslashes() function for ALL queries on my database (such as login), does that mean that my site is completely secure from SQL injections and other threats or is this only the start of it.
     
    oo7ml, Jun 23, 2007 IP
  2. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    CodyRo, Jun 23, 2007 IP
  3. DeViAnThans3

    DeViAnThans3 Peon

    Messages:
    785
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You could use mysql_real_escape_string; or addslashes. But if you use addslashes, also add an htmlspecialchars() (well, that's what I usually do) :)
     
    DeViAnThans3, Jun 24, 2007 IP
  4. iRAY

    iRAY Peon

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    In case you are using MySQL, your site will be more secure with mysql_real_escape_string() - it is good for strings.
    For numbers (e.g. ID's in database) is better intval(), because is much more faster.
     
    iRAY, Jun 24, 2007 IP
  5. priyakochin

    priyakochin Banned

    Messages:
    4,740
    Likes Received:
    138
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Can I use mysql_real_escape_string() before inserting to the db ?
     
    priyakochin, Jun 24, 2007 IP
  6. chuckd1356

    chuckd1356 Active Member

    Messages:
    770
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    70
    #6
    Obviously, that's what it's there for. You will be happy you did.

    <?php
    require_once 'includes/conf.inc.' . $phpex;
    $var = $_POST['foo'];
    $var = mysql_real_escape_string($var);
    
    $insert_id = mysqli_query(...) or die(...);
    
    //See?
    
    ?>
    
    
    
    PHP:
    :)
     
    chuckd1356, Jun 24, 2007 IP
  7. OIOplus

    OIOplus Peon

    Messages:
    233
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You could also create some custom functions that do "the essentials" for most of your inputs.

    For instance:

    
    function clean_str($input) {
    	$output = strip_tags($input);
    	$output = trim($output);
    	$output = mysql_real_escape_string($output);
    	return $output;
    }
    
    Code (markup):
    I use that for strings that I want to make sure don't have any tags inserted in them, or could be the subject of sql injections. Obviously, it assumes you already have a db connection open when calling the function, and you want to strip tags too.
     
    OIOplus, Jun 24, 2007 IP