Solved

escaping input into db

Posted on 2009-07-13
4
176 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Exchange server is not supported in any cloud-hosted platform (other than Azure with Azure Premium Storage).
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this video we show how to create an Address List 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 Organization >> Ad…
To show how to generate a certificate request 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 Servers >> Certificates…

708 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

17 Experts available now in Live!

Get 1:1 Help Now