473,803 Members | 4,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

addslashes/mysql_real_esca pe_string

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_esca pe_string()?

Thanks you.

Regards

ndlarsen
Mar 27 '08 #1
13 3489
In our last episode, <47************ ***********@new s.sunsite.dk>, the lovely
and talented ndlarsen broadcast on comp.lang.php:
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_esca pe_string()?
See the best practices example in the article on mysql_real_escp ae_string in
the manual. For portability you need to check for whether magic quotes are
on and reverse them if they are. If portability is not a concern and it is
your own machine, you can turn magic quotes off and save a few steps.

mysql_real_escp ae_string requires a database connection and will attempt to
establish one if it cannot find an explicit link or the default previous
link. The most convenient time, then, to apply it is just before entering
the data in the database.
--
Lars Eighner <http://larseighner.com/us****@larseigh ner.com
Countdown: 299 days to go.
Mar 27 '08 #2
ndlarsen wrote:
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_esca pe_string()?
Depends on what database you're using. mysql_real_esca pe_string() is a
good solution if you're using MySQL, because it uses encoding-specific
techniques.

Some of the other database modules provide similar functions. For those
that don't, addslashes() can be used as a last resort.

The best solution though is to use PDO and prepared statements.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 20:40.]

Best... News... Story... Ever!
http://tobyinkster.co.uk/blog/2008/03/23/hypnotist/
Mar 27 '08 #3
I appreciate your reply.
Depends on what database you're using. mysql_real_esca pe_string() is a
good solution if you're using MySQL, because it uses encoding-specific
techniques.
Sorry about that, I should have mentioned that it is a MySQL database.
The best solution though is to use PDO and prepared statements.
You sort of lost me here.

Regards

ndlarsen
Mar 27 '08 #4
In our last episode, <47************ ***********@new s.xs4all.nl>, the lovely
and talented Erwin Moller broadcast on comp.lang.php:
No matter if you are updating, or inserting, or selecting: You ALWAYS must
prepare for the worst.
This is, of course, the right answer. The database can be attacked through
a query string that you intend for SELECT --- which if it worked as you
intended, would only obtain data from the database. But of course a
malicious value could turn your SELECT query into anything else, so you must
escape any value that could possibly be tainted --- and generally escaping
every value whatsoever is best.

--
Lars Eighner <http://larseighner.com/us****@larseigh ner.com
Countdown: 299 days to go.
Mar 27 '08 #5
ndlarsen wrote:
Again I appreciate your replies.
I guess I chose the wrong words before, probably because I knew what I
meant and expected that everybody else would as well. Now, as none is
capable of mind reading, this is not so. ;)
I just tested the "best practices" code from the php manual and it did
what I expected it to - that is, not modifying the data and submitting
it to the db unaltered. So if I were to submit data to a db, a string
containing "jnjsdf'jn/ljknv\knns", it would be submitted to the db as
such and I could retrieve it without the need to strip the string from
any sort of characters, right? At least so it seems from following code:
Read up on mysql_real_esca pe_string(). It does modify the data as it's
being sent to the database - but the data is modified in a predictable
way (i.e. to take care of embedded quotes, etc.). The result when
retrieved from the database is just as you put it in there.

Add databases require some modification of the data to store special
characters. But if you do the modification properly, the data is
retrieved without modification. MySQL has a function which does this
for you; some others don't.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Mar 27 '08 #6
..oO(ndlarsen)
>The best solution though is to use PDO and prepared statements.

You sort of lost me here.
See the PDO section in the PHP manual. It's really not that complicated,
but much more powerful and flexible.

Micha
Mar 27 '08 #7
Jerry Stuckle wrote:
Read up on mysql_real_esca pe_string(). It does modify the data as it's
being sent to the database - but the data is modified in a predictable
way (i.e. to take care of embedded quotes, etc.). The result when
retrieved from the database is just as you put it in there.
Add databases require some modification of the data to store special
characters. But if you do the modification properly, the data is
retrieved without modification. MySQL has a function which does this
for you; some others don't.
Right, I think I got it now. Some of the testing code I used made it
appear as if the string was automatically stripped of backslashes when I
retrieved it from the database/table.
If I run mysql_real_esca pe_string() on a string prior to inserting it
into a database/table, it is submitted to the database modified (with
backslashes escaping special characters). If I retrieve that same string
from the database/table, it is still modified and I need to strip the
string of the backslashes, perhaps with stripslashes()?

Thank you.

Regards

ndlarsen
Mar 27 '08 #8
Michael Fesser wrote:
See the PDO section in the PHP manual. It's really not that complicated,
but much more powerful and flexible.
Will do, thanks.

ndlarsen
Mar 27 '08 #9
On Mar 27, 9:39*am, ndlarsen <use...@ionline .dkwrote:
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_esca pe_string()?

Thanks you.

Regards

ndlarsen
I don't want to go offtopic, but mysql_real_esca pe_string serously
leaks memory for me. Using MDB2's quote function it runs out of my
allowed 200MB in a minute. If I just comment out
mysql_real_esca pe_string inside the escape function there is no leak.
Any idea?
Mar 27 '08 #10

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

Similar topics

1
3162
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, Lee G. This is Aviva\'s website. This is Sarah's website.
0
1704
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, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue; switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
4
18981
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". By default, magic_quotes_gpc is On. If magic_quotes_gpc is On: Use stripslashes() for data which originates from an HTML form and is shown in an HTML page after a roundtrip to the server. Do not use addslashes().
2
7643
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 mysql_real_escape_string() or addslashes().
2
3372
by: Cruella DeVille | last post by:
I must have som errors in my understanding of strip- vs addslashes. I thought that if a user submitted eg a username, like this username=siv' drop database test; I should addslashes to escape ' and " and therefore prohibit the evil user to drop/change my database through sql injection (my example may not be correct, but I believe it points out that evil user can add sql commands through an input field. But - I've been reading lots of...
15
4150
by: =?ISO-8859-1?Q?J=F8rn?= Dahl-Stamnes | last post by:
Hello folks, I need some help/advice FAST. I have problems with addslashes on my web-servers. After uploading a file, I read the uploaded file, use addslashes on the read data and then insert it into a blob field in a MySQL database. The problem is that this works fine on my internal test web-server (running under RedHat 7.3). But on my production web-server (running Fedora Core 4)
6
3779
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 - magic_quotes_gpc sounds ideal but is not an option as I don't have access to the php.ini file and it is currently set to 0. I could use either addslashes or mysql_real_espcape_string but do I have to apply this to every field individually or is there a...
5
6639
by: Gilles Ganault | last post by:
Hello As the user may type strings that contain verboten characters like apostrophes, I need to go through the $_POST array, and use addslashes() on each and every item But it doesn't make any difference: ========== <?php
8
4027
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 database as we’re. weird. I've used addslashes & mysql_real_escape_string, but nothing seems to fix it, and I can see when I 'echo' the input, that the string is not being escaped. I don't get any errors, but nothing happens.
0
9564
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10316
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9125
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
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
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.