tags:

views:

1513

answers:

5

Addslashes seems to be a bit confused. Given the following 2 lines of code

$name = "Dave's test";
$newName = addslashes($name);

I am expecting $newName to be "Dave\'s test" (my one single quote nicely escaped)

However, what I'm getting is "Dave\\'s test" (note the DOUBLE backslashes). This contradicts every bit of online documentation I can find on addslashes - and causing me a lot of grief.

I am dumping the before and after addslashes results to the http error log via error_log...

error_log("before=$name  after=$newName");

results...

before=Dave's test  after=Dave\\'s test

Note - this is part of an ajax process, so I can't really 'echo' the results.

Any insights into why addslashes would be double up on the backslases are much appreciated.

FYI: I'm Using PHP 5.2.6 under linux with magic quotes OFF.

A: 

My guess is that the error_log() functionality is escaping the backslash itself with another backslash.

The string that would then represent would technically only have 1 backslash in it, which is the desired result.

Try this as a simple test:

error_log('\a')

and see if you get \\a in the log

John Rasch
No. This is actually part of an input process - this isn't being re-POSTed
P.Scivetti
PHP strings need to be escaped...
strager
Just noticed that, thanks
John Rasch
+1  A: 

For starters, why are you escaping with addslashes()? It's an insufficient method at best, especially if you're trying to guard against SQL injection.

What else can you tell us about your configuration so we can try and replicate?

Kalium
+1 - you should add mysql_real_escape_string() to your post to clarify
John Rasch
If there is any possibility you are using a non-ascii-based multi-byte encoding, ie anything other than ASCII, ISO-8859-x, UTF-8, then you need to use myqsl_real_escape_string(). Otherwise it is pretty much the same as addslashes(). I don't think this is very relevant to the question.
thomasrutter
@Rasch, SQL injection is an example. He could be passing JSON (OP mentions Ajax), in which case he should be using json_encode. There are many other functions for different applications.
strager
@thomasrutter It's about the usage scenario. Why is addslashes() even being used in the first place? If it's escaping for interpolation into a SQL query, then the user is doing in wrong and a more DB-specific function is needed. Or ADODB. Or PDO.
Kalium
@strager - right on, I always assume if someone uses PHP that they're using MySQL as well.
John Rasch
1) He is not necessarily outputting to MySQL anyhow, so we can't jump to conclusions. 2) It really isn't relevant to the question anyhow. I can't see how the problem is related to using addslashes().
thomasrutter
A: 

Unable to replicate on 5.2.6 or 5.2.0 installs.

Are you seeing the double backslash if you echo $newName immediately after the operations you wrote? I ask because I suspect the intervention of some other processing layer that is doubling the backslash, not addslashes().

chaos
Good question. I added more detail to the original question to clarify how I'm seeing the error (which is via error_log). This is an ajax-based process, so I'm not really able to 'echo' anything without messing up the transaction.
P.Scivetti
A: 

Note - this is part of an ajax process, so I can't really 'echo' the results.

I think that may be the key to the problem - I would think that the addslashes is probably correctly adding a single backslashes to that quote, but that somehow your AJAX process is escaping it again. Are you passing this text via JSON (using json_encode)? If so, you don't need to escape it, as json_encode() will do that transparently.

If you can't 'echo' the result, perhaps write it to a file?

$name = "Dave's test";
$newName = addslashes($name);
file_put_contents('/tmp/test.txt', $newName); // temporary

This could at least let you prove to yourself that your addslashes() is doing what it is supposed to.

thomasrutter
A: 

Looks like error_log is calling addslashes internally. After reading the questions posted in reponse to my original question, I created a very trivial script...

<?php
        $name = "Dave's test";
        $newName = addslashes($name);
        echo    "name=$name.   newName=$newName";
        error_log("name=$name.   newName=$newName");
?>

Result from the echo:

name=Dave's test. newName=Dave\'s test

Result from the error_log:

name=Dave's test.   newName=Dave\\'s test

Many thanks to all who took the time to read and comment on this question. This was my first question on Stack Overflow and I was just blown away by the speed of the responses. What a great community!

P.Scivetti