Solved

escaping input into db

Posted on 2009-07-13
4
177 Views
Last Modified: 2012-05-07
Is the following function good enough to make sure my php app doesn't get hacked?
also will this allow html to be displayed corretly?
its for CMS so its has to deal with just about any kind of input.
function db_escape($values, $quotes = true) {

    if (is_array($values)) {

        foreach ($values as $key => $value) {

            $values[$key] = db_escape($value, $quotes);

        }

    }

    else if ($values === null) {

        $values = 'NULL';

    }

    else if (is_bool($values)) {

        $values = $values ? 1 : 0;

    }

    else if (!is_numeric($values)) {

        $values = mysql_real_escape_string($values);

        if ($quotes) {

            $values = '"' . $values . '"';

        }

    }

    return $values;

}

Open in new window

0
Comment
Question by:casit
  • 2
  • 2
4 Comments
 
LVL 4

Expert Comment

by:Xemorph
ID: 24846203
It looks fine to me.  I think it will prevent any SQL injection attacks, I don't know about making your app hack proof.

I would add cases for int or float and use intval() and floatval().  Just to cover all cases.
0
 

Author Comment

by:casit
ID: 24846225
and do what with them?
0
 
LVL 4

Accepted Solution

by:
Xemorph earned 500 total points
ID: 24846247

function db_escape($values, $quotes = true) {

    if (is_array($values)) {

        foreach ($values as $key => $value) {

            $values[$key] = db_escape($value, $quotes);

        }

    }

    else if ($values === null) {

        $values = 'NULL';

    }

    else if (is_bool($values)) {

        $values = $values ? 1 : 0;

    }

    else if (!is_numeric($values)) {

        $values = mysql_real_escape_string($values);

        if ($quotes) {

            $values = '"' . $values . '"';

        }

    }
 

    // ADD THIS

    else if(is_float($values){

        $values = floatval($values);

    }

    else

        $values = intval($values);

    }
 

    // End with an else so we have a case for all possiable inputs.

    // If you find another possiable input, add it to the else if list.

    // I would always end on an else though, incase you did 

    // miss something
 

    return $values;

}

Open in new window

0
 

Author Comment

by:casit
ID: 24876494
thanks
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…
The viewer will learn how to count occurrences of each item in an array.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now