473,396 Members | 2,026 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,396 software developers and data experts.

Addslashes() doesn't work on $_POST

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
switch ($_POST['status']) {
case "Test":
print $_POST['dummy'] . "<p>\n";

foreach ($_POST as $key =$value)
$$key = addslashes($value);

print $_POST['dummy'] . "<p>\n";

$sql = sprintf("INSERT INTO mytable VALUES
('%s')",$_POST['dummy']);
print "$sql<p>";

/*
Bill's cigar

Bill's cigar

INSERT INTO mytable VALUES ('Bill's cigar')
*/

break;

default:
echo "<form method=post>";
echo "<input type=hidden name=dummy value=\"Bill's cigar\">";
echo "<input type=submit name=status value=Test>";
echo "</form>";
break;
}
?>
==========

What am I doing wrong?

Thank you.
Feb 19 '08 #1
5 6600
What am I doing wrong?
>
Thank you.
Firstly, using a variable variable ($$) won't update the superglobal
$_POST, it just creates a new variable - in this case $dummy.

You can update the superglobal itself, i.e., $_POST['dummy'] =
addslashes($_POST['dummy']). Your loop would then be:

foreach($_POST as $key =$value)
{
$_POST[$key] = addslashes($value);
}

Secondly, using addslashes to quote data going into an SQL query isn't
a very good idea. If you're running PHP 5.1 (or higher) I would
strongly suggest using PDO and the prepare/bind syntax. Otherwise, if
using the mysql*_* set of functions use mysql_real_escape_string
(similar functions exist for the other databases supported by PHP)

Finally, you are outputting data straight to the browser with your
print commands; I'm sure this is just for debugging purposes, however
you really should take XSS attacks into account and filter the input
accordingly. For instance, addslashes cannot save you from something
like this:

<script type=text/javascript src=http://www.example.com/
someevilscript.js></script>

Hope that helps.
Feb 19 '08 #2
ph******@gmail.com wrote:
>What am I doing wrong?

Thank you.

Firstly, using a variable variable ($$) won't update the superglobal
$_POST, it just creates a new variable - in this case $dummy.
More precisely, it's supposing that the $_POST variables are also defined in
the global scope.

That behaviour was the default in old versions of PHP (Register_globals =
On). Now it's off by default for security reasons.

My guess is that you copy-pasted some old code from somewhere without
understanding it first ;-)

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

By trying we can easily learn to endure adversity. Another man's, I mean.
-- Mark Twain

Feb 19 '08 #3
ph******@gmail.com wrote:
>
Secondly, using addslashes to quote data going into an SQL query isn't
a very good idea. If you're running PHP 5.1 (or higher) I would
strongly suggest using PDO and the prepare/bind syntax. Otherwise, if
using the mysql*_* set of functions use mysql_real_escape_string
(similar functions exist for the other databases supported by PHP)
When using PDO you mean the prepare insert statement should be used? Can
you please give a small example?

Thanks
Feb 19 '08 #4
..oO(Gilles Ganault)
>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
No, you don't need to apply addslashes() to each and every item. Instead
you should consider $_GET and $_POST read-only and use the appropriate
escaping functions when and where they're really needed, for example
mysql_real_escape_string() when inserting the data into a MySQL DB (in
this case prepared statements would be the better way, though).

IMHO the only acceptable write-access to these arrays is stripslashes()
to remove magic quotes if they're enabled and can't be turned off. But
besides that they shouldn't be touched and just be seen as the raw data
input. The escaping takes place when the data is used.

Micha
Feb 19 '08 #5
On Mon, 18 Feb 2008 16:42:35 -0800 (PST), ph******@gmail.com wrote:
>Secondly, using addslashes to quote data going into an SQL query isn't
a very good idea. If you're running PHP 5.1 (or higher) I would
strongly suggest using PDO and the prepare/bind syntax.
Thanks guys. For those interested, here's some working code, using
either bindParam() or an array:

<?php
switch ($_POST['status']) {
case "Test":
$dbh = new PDO("sqlite:test.sqlite");

//Good
//$sql = "INSERT INTO mytable VALUES (:dummy)";
//$stmt = $dbh->prepare($sql);
//$stmt->bindParam(":dummy", $_POST['dummy']);
//$insert->execute();

try {
$insert = $dbh->prepare("INSERT INTO mytable (dummy) VALUES
(?)");
$insert->execute(array($_POST['dummy']));
} catch (Exception $e) {
echo "Failed : " . $e->getMessage();
}

$dbh = null;
break;

default:
echo "<form method=post>";
echo "<input type=text name=dummy>";
echo "<input type=submit name=status value=Test>";
echo "</form>";
break;
}
?>
Feb 19 '08 #6

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

Similar topics

5
by: Google Mike | last post by:
I have RH9 Linux with the versions of Apache and PHP that came with it. The PHP is version 4.2.2 on the CD, I believe. Apache, I think, is version 2.0. I found I can do some regular PHP stuff...
19
by: Chris Allen | last post by:
Hi I'm new to PHP and I'm trying to create a Login Form. Once the user has logged in then he shouldn't have to log in again. The trouble is I'm getting a new session ID between every page and so...
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"....
11
by: Dave Smithz | last post by:
Having adopted someone else's PHP cope and completing a crash course in the language I came across a (probably common) problem with the current code. On a registration form, whenever users names...
2
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...
8
by: warezguy05 | last post by:
Hi I'd like to forward users to a 'thank-you' page after they've submitted a form. I used this code and it worked perfectly till yesterday; header("Location:...
7
by: Chuck Anderson | last post by:
I am trying to implement email injection protection by looking for \r and/or \n in the name, subject, or email address fields from my contact form The first script, contact_us.php, contains a...
5
by: lawrence k | last post by:
This seems so simple, I can't believe its tripping me up. I've a database class with a query method that looks like this: function query($query=false) { global $controller; // $query =...
3
by: whitey | last post by:
this code is producing the message BUT it is entering the data. What should i do? <?php if (!$_POST) { //haven't seen the form, so show it $display_block = " <form...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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...
0
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...
0
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...
0
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,...

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.