473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

mysql_real_escape_string() vs addslashes()

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 mysql_real_escape_string() or addslashes().

My question is this... from what I can gather on php.net and some other
sources, mysql_real_escape_string() is better than addslashes(), so am I
correct in saying that I don't ever need to use addslashes()?

I know I need to use one of these functions when formatting queries to
MySQL to prevent SQL injection attacks, but how about when I am just
dealing with variables in $_POST, $_GET, and $_SESSION? With magic
quotes on, when I perform a SELECT and a row has a single quote in the
result, for example, magic quotes will automatically add a \ to the
value. Is there any security risk or other drawback in not escaping out
special characters that I am just working with in the code, and then
formatting everything right before sending to the database?

Thanks a lot in advance.
Nov 3 '05 #1
2 7602
Marcus wrote:

Hi Marcus,
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.
Why is that?
I think you should decide for yourself what you like the best.
You can always just overrule the ini-settings by:
ini_set("magic_quotes_gpc" , "1");

Are you maybe confusing magic_quotes_gpc with magic_quotes_runtime?

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 mysql_real_escape_string() or addslashes().
yes.

My question is this... from what I can gather on php.net and some other
sources, mysql_real_escape_string() is better than addslashes(), so am I
correct in saying that I don't ever need to use addslashes()?
I am unsure why the former is better, but if you only use the POST/GET data
on mySQL, yes: you do not need to add or strip slashes, you could just use
mysql_real_escape_string() .


I know I need to use one of these functions when formatting queries to
MySQL to prevent SQL injection attacks, but how about when I am just
dealing with variables in $_POST, $_GET, and $_SESSION?
If you are getting data from POST/GET/COOKIE, you need to look at
magic_quotes_gpc.

If you want data coming from queries to be escaped, use
magic_quotes_runtime.

I don't think the last one is very handy in most situations..
I always turn it off.

With magic quotes on, when I perform a SELECT and a row has a single quote in the
result, for example, magic quotes will automatically add a \ to the
value. Is there any security risk or other drawback in not escaping out
special characters that I am just working with in the code, and then
formatting everything right before sending to the database?

Thanks a lot in advance.


Well, have a look at BOTH magic_quotes functions, and your confusion will
disappear. :-)

Regards and good luck!

Erwin Moller
Nov 3 '05 #2
Marcus wrote:
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 mysql_real_escape_string() or addslashes().

My question is this... from what I can gather on php.net and some other
sources, mysql_real_escape_string() is better than addslashes(), so am I
correct in saying that I don't ever need to use addslashes()?

I know I need to use one of these functions when formatting queries to
MySQL to prevent SQL injection attacks, but how about when I am just
dealing with variables in $_POST, $_GET, and $_SESSION? With magic
quotes on, when I perform a SELECT and a row has a single quote in the
result, for example, magic quotes will automatically add a \ to the
value. Is there any security risk or other drawback in not escaping out
special characters that I am just working with in the code, and then
formatting everything right before sending to the database?

Thanks a lot in advance.


If you are only concerned with MySQL queries, then *only* use
mysql_real_escape_string.

It escapes special characters in the string using the current character
set of the connection. If you want to use binary data in your query, you
will definately need this function as well. PHP's
mysql_real_escape_string uses MySQL's library function
mysql_real_escape_string.

The same holds true for all database systems when working with PHP. If
there is a "native" escaping function, you should use that and only use
addslashes as a last resort.

addslashes only adds a backslash for the following characters:
* single quote (')
* double quote (")
* backslash (\)
* NUL (the NULL byte).

mysql_real_escape_string escapes the folowing characters:
* \x00
* \n
* \r
* \
* '
* "
* \x1a

HTH.

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Nov 3 '05 #3

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

Similar topics

1
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....
0
by: Bob Bedford | last post by:
I've to put datas from user's input in a database. I've taken a function from internet (don't remember where) formatting most of the values: function GetSQLValueString($theValue, $theType,...
4
by: Jan Pieter Kunst | last post by:
Q. How do I use addslashes() and stripslashes() when dealing with HTML forms and database INSERTs, UPDATEs and SELECTs? A. It depends on the setting of the php.ini directive "magic_quotes_gpc"....
2
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...
7
by: Paul Furman | last post by:
mysql_real_escape_string() is apparently chopping off anything that follows a quote when I grab the data & put it in a form for editing. Sorry if I'm not explaining this properly, I'm pretty...
6
by: redog6 | last post by:
Hi I have a webform with many free text fields and have a problem with apostrophes and single quotes as this breaks the mysql query string. I obviously need to escape these characters -...
13
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...
8
by: pedalpete | last post by:
I am finding this very strange and frustrating, but I've got some data being entered into a mysql database, and when the data contains an apostrophe for example the word we're, it shows up in the...
7
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 =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.