Solved

escaping input into db

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
This article explains how to install and use the NTBackup utility that comes with Windows Server.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager

831 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