473,549 Members | 2,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mysql_real_esca pe_string();

Is there really any time when I don't want to run every _POST and _GET
through mysql_real_esca pe_string() before I use that data in accessing
the database?

In other words, is there a good reason why I shouldn't have a function
that walks through the POST[] and GET[] arrays and processes the
mysql_real_esca pe_string() function against the data in order to ensure
that there will be no attempts to do an SQL inject?

My thinking is that this function could be run at the top of my page
init and in doing so it will ensure that there can be no sql injection.
Am I missing something "very bad" that this could do instead?
function cleanall()
{
foreach($_POST as $key =$val)
{
$_POST[$key] = stripslashes(st rip_tags(htmlsp ecialchars($val , ENT_QUOTES)));
$$key = stripslashes(st rip_tags(htmlsp ecialchars($val , ENT_QUOTES)));
}
foreach($_GET as $key =$val)
{
$_GET[$key] = stripslashes(st rip_tags(htmlsp ecialchars($val , ENT_QUOTES)));
$$key = stripslashes(st rip_tags(htmlsp ecialchars($val , ENT_QUOTES)));
}
}
Feb 12 '07 #1
4 3108
JM Ivler wrote:
Is there really any time when I don't want to run every _POST and _GET
through mysql_real_esca pe_string() before I use that data in accessing
the database?

In other words, is there a good reason why I shouldn't have a function
that walks through the POST[] and GET[] arrays and processes the
mysql_real_esca pe_string() function against the data in order to ensure
that there will be no attempts to do an SQL inject?

My thinking is that this function could be run at the top of my page
init and in doing so it will ensure that there can be no sql injection.
Am I missing something "very bad" that this could do instead?
function cleanall()
{
foreach($_POST as $key =$val)
{
$_POST[$key] = stripslashes(st rip_tags(htmlsp ecialchars($val ,
ENT_QUOTES)));
$$key = stripslashes(st rip_tags(htmlsp ecialchars($val , ENT_QUOTES)));
}
foreach($_GET as $key =$val)
{
$_GET[$key] = stripslashes(st rip_tags(htmlsp ecialchars($val , ENT_QUOTES)));
$$key = stripslashes(st rip_tags(htmlsp ecialchars($val , ENT_QUOTES)));
}
}
Integers should be validated with either an (int) cast or the intval()
function.
Feb 12 '07 #2
JM Ivler wrote:
Is there really any time when I don't want to run every _POST and _GET
through mysql_real_esca pe_string() before I use that data in accessing
the database?
Well, if ALL your data posted to you in the form is ment to be inserted in a
mySQL database, then it comes in handy, maybe.
If the data is ment for anything else, it should be treated that way.

I would suggest that you only call mysql_real_esca pe on data that you are
going to use in your databasestateme nt, and leave the superglobals alone.

And as Curtis said: If you expect an integer, treat it like that, eg:
$userid = (int)$_POST["userid"];

Always completely scrubbing the POST and GET array sounds like overkill to
me, and could lead to bugs in your code. Just call the real escape when and
where you need it.

On a sidenote (and I don't want to sound teacherlike): Paranoid is
completely acceptable, even desirable, when processing client data in a
database.
Just make sure you know WHERE you do WHAT, and WHY you do it.

I want to emphazise that point because I have seen a LOT of (often bad)
postings in all kind of fora where people post a 'safe insert' without even
paying attention to ini-settings or giving a detailed description of the
situation.
If people start using that code they are lured into a false sense of
security.
Being the PHP coder, you are the last line of defense against hackattacks,
and you should pay attention to each query that contains possibly tainted
data.
Using a function like the one you suggest may easily lead to a 'lazy
attitude' because all your data is safe for insert.

Just my 2 cent.

Regards,
Erwin Moller

>
In other words, is there a good reason why I shouldn't have a function
that walks through the POST[] and GET[] arrays and processes the
mysql_real_esca pe_string() function against the data in order to ensure
that there will be no attempts to do an SQL inject?

My thinking is that this function could be run at the top of my page
init and in doing so it will ensure that there can be no sql injection.
Am I missing something "very bad" that this could do instead?
function cleanall()
{
foreach($_POST as $key =$val)
{
$_POST[$key] = stripslashes(st rip_tags(htmlsp ecialchars($val ,
ENT_QUOTES))); $$key = stripslashes(st rip_tags(htmlsp ecialchars($val ,
ENT_QUOTES))); }
foreach($_GET as $key =$val)
{
$_GET[$key] = stripslashes(st rip_tags(htmlsp ecialchars($val ,
ENT_QUOTES))); $$key = stripslashes(st rip_tags(htmlsp ecialchars($val ,
ENT_QUOTES))); }
}
Feb 12 '07 #3
JM Ivler wrote:
In other words, is there a good reason why I shouldn't have a function
that walks through the POST[] and GET[] arrays and processes the
mysql_real_esca pe_string() function against the data in order to ensure
that there will be no attempts to do an SQL inject?
Yes -- firstly there may be (often is) things in those arrays that you
don't have any intention of putting into a database, and ,ay wish to do
something else with instead. Running mysql_real_esca pe_string on them is
annoying when you try to use the variable for something else, and also a
waste of CPU time.

Secondly, many values can be sanitised using other methods that are less
CPU-intensive. For example, if you have a string that you need to insert
into a database, and you know that this string must consist of
alphanumeric characters only, then you can sanitise it like this:

$var = preg_match('/[^A-Za-z0-9]/', '', $var);

If you have a variable you know should be an integer:

$var = (int)$var;

and so on. mysql_real_esca pe_string() (and the equivalent functions for
the better databases ;-) ) should only be used when you know that you
couldn't do a better job of sanitising the data yourself.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 15 '07 #4
Is there really any time when I don't want to run every _POST and _GET
through mysql_real_esca pe_string() before I use that data in accessing the
database?

In other words, is there a good reason why I shouldn't have a function
that walks through the POST[] and GET[] arrays and processes the
mysql_real_esca pe_string() function against the data in order to ensure
that there will be no attempts to do an SQL inject?
You should be validating user input before you put it into the database and
using that fucntion at the top of your script will hinder your validation
attempts (as you will end up with escape characters in the string). If for
example you ask someone their age in a form ensure it is an int. If it is an
int then there is no need to use that function on it.
Feb 23 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3122
by: leegold2 | last post by:
When I look directly in my db field I see a difference between these two functions. The top line (seebelow) was inserted with addslashes vs. the bottom line where I used mysql_real_escape_string. Obviously addslashes really escapes the apostrophe. But I thought mysql_real_escape_string is supposed to do that too - can anyone explain? Thanks,...
1
1873
by: Michael G | last post by:
If I only escape the characters that mysql_real_escape_string recognizes, is this adequate protection against SQL injection attacks? I have read a number of archived posts plus I've read some of the info at php.net. I am still not convinced as to what to do. The php folks claim that using mysql_real_escape_string is all that is needed. Then...
2
7619
by: Marcus | last post by:
Hello, My php.ini file currently has magic quotes set to On, but I have read that it is better to code with it off. Currently with magic quotes on, I only use stripslashes() to properly format strings that are displayed on the screen. I know that now with magic quotes off, I will have to manually handle escaping special characters with...
9
1987
by: frizzle | last post by:
Hi groupies, I have 2 small questions, which i really want to be sure about: - does mysql_real_escape_string() prevent any kind of mysq-injection? - can it put escaped quotes etc in the DB, or are those automatically 'converted' again? Thanks!
2
29525
by: comp.lang.php | last post by:
when trying to use the mysql_real_escape_string() function, the following warning occurs: First of all, the user is not 'web' trying to connect to the database, secondly, what is mysql_real_escape_string() doing connecting to the database, as 'web' or anyone else, and thirdly, why is this happening, does anyone know?
2
2896
by: matthud | last post by:
<?php //MAKE IT SAFE $chunk = $_POST; $title = $_POST; $url = $_POST; $tags = $_POST; $user = $_POST; $safe_chunk = mysql_real_escape_string(htmlentities($chunk)); $safe_title = mysql_real_escape_string(htmlentities($title));
11
2722
by: zach | last post by:
I created a comment form which will inserts the comments into a database and displays them immediately. I want to make sure that its safe from users inserting unwanted data into the database or executing queries. Here's my php code, is this done right? Is there anything else I should to to make it more secure? $handle =...
2
3370
by: Pugi! | last post by:
It is by accident that I noticed that I forgot to use mysql_real_escape_string in part of my webapp. I tested input with following text : Hélène 51°56'12'' http://www.mysite.org/folder 3 functions worked correctly and 1 failed: The one that failed didn't have mysql_real_escape_string and neither did 2 of the ones that worked: in those 2 I...
13
3456
by: ndlarsen | last post by:
Hello. It's been a while since I used php. Since then magic quotes has been deprecated and will be removed when php 6.0 hits. My question is, what should I be using when submitting data to a database instead? Which is better for security reasons, addslashes() or mygql_real_escape_string()? Thanks you. Regards
7
5139
by: roseple | last post by:
Hi, can anyone please help me why I got this error every I uploaded files. Error: Here is the code on the said warning message: # Gather all required data $name = mysql_real_escape_string($dbLink, $_FILES); $mime = mysql_real_escape_string($dbLink, $_FILES); $size = $_FILES;
0
7542
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7467
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7500
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7827
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6066
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5385
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3494
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1961
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.