Forum Moderators: coopster

Message Too Old, No Replies

Form submits backslashes

A form submitted to a PHP script adds backslashes before quotes

         

webfoo

3:35 pm on Mar 24, 2008 (gmt 0)

10+ Year Member



I'm developing a simple custom CMS in PHP. For the page editor, I have a textarea form that submits to a PHP. THe PHP is supposed to write the contents of the textarea to a .inc file. The problem is that a \backslash gets added before each 'quote' or "double quote". This is a problem because when the .inc file gets read, the html could look like
<div class=\"pagetitle\">
which the browser does not display properly.

I've heard of stripslashes(), but not been successful with it.

I tried making a client-side JavaScript that replaces each quote with &#49; and each double-quote with &quot;. It would only replace the first quote and the first double quote in the submission.

Any ideas?

penders

5:17 pm on Mar 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The problem is that with 'magic quotes' turned on (which it usually is by default in the PHP configuration), all POST (and GET) vars are escaped with slashes; the single-quotes and double-quotes (and backslashes) get escaped with a backslash. In other words addslashes() [uk.php.net] is automatically applied.

This may be OK if you need to use this value in order to build a SQL statement to perhaps insert the value into a DB, but otherwise it's a pain!

(I have read that you should never need to call stripslashes() in this instance - but I think this may have been in a discussion regarding SQL (as above) - anyway...)

I have had this same problem and I have used stripslashes() [uk.php.net] to successfully remove the slashes before saving the textarea content to a file. It works.

In getting the textarea CONTENT, I have done the following:

$CONTENT = isset($_POST["content"]) ? $_POST["content"] : null; 
if (!is_null($CONTENT) && get_magic_quotes_gpc()) {
$CONTENT = stripslashes($CONTENT);
}

NB: Apparently magic quotes have been completely removed from PHP6, along with the function get_magic_quotes_gpc() [uk.php.net]!

webfoo

9:07 pm on Mar 24, 2008 (gmt 0)

10+ Year Member



That code does not seem to be working. I tried it out. Then I tried replacing your POST commands with REQUEST's because sometimes that works for me. But it did not help. Here's the code:

<?
$pagename=$_REQUEST["p"]; //Request the variable p from the URL

$CONTENT = isset($_REQUEST["content"]) ? $_REQUEST["content"] : null; if (!is_null($CONTENT) && get_magic_quotes_gpc()) { $CONTENT = stripslashes($CONTENT); } //Pender's code

$fullpage= "../" . $pagename . ".inc"; //creates the full URL path for the .inc file
$file=fopen($fullpage,"w"); //opens the .inc file
echo fwrite($file,$content); //writes the content to the .inc file
fclose($fullpage); //closes the file

echo "(Some stuff that lets the user know the page has been updated.)"; //self-explanatory

?>

penders

8:32 am on Mar 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Which superglobal you'd use depends on how you are submitting your form. If method="post" then use $_POST[], if method="get" then $_GET[]. The $_REQUEST[] superglobal is an amalgamation of $_GET, $_POST, and $_COOKIE so should always work as well. By the sounds of it you are using a GET request (the values appear in the URL).

There are size limitations with the GET method, however. If you are posting back a large block of text it may be better to use method="POST" ?

$_REQUEST["content"]

Presumably you have a textarea field with name="content"? My code is an example, you may need to tailor it to suit.

Also, you are assigning to $CONTENT (uppercase), but you are writing out $content (lowercase) - two different variables!

penders

12:16 pm on Mar 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The $_REQUEST[] superglobal is an amalgamation of $_GET, $_POST, and $_COOKIE so should always work as well.

Just a thought... security. One reason to use the correct $_GET or $_POST rather than $_REQUEST is security.

For instance, if your form submits the data back as post data and in your code you are checking something like:

if (isset($_REQUEST["username"])) { // Do something }

Even if your form did not submit anything, a hacker could simply append "

?username=bob
" to the URL and bobs your uncle, your code 'does something' (because 'username' now appears in the $_GET[] array which is part of $_REQUEST[]). (simple code injection)

webfoo

6:58 pm on Mar 25, 2008 (gmt 0)

10+ Year Member



THANK YOU SO MUCH, Penders! Your point about upper/lower case variables turned out to be the issue. It now works.

I am acually using a combination of POST and GET. The action of the form is set to "update.php?p=pagename" where pagename is the name of the page you are editing (That uses GET). Then the textarea is POSTed because of the issues mentioned above.

I see your point about security. Normally, I would be concerned about this, but the entire directory is password-protected so only persons with a username and password can access it.

Thanks again!
Cheers,
Webfoo.

PS: For the record, the final code I used is:

<?
$pagename=$_REQUEST["p"];

$content = isset($_REQUEST["content"]) ? $_REQUEST["content"] : null; if (!is_null($content) && get_magic_quotes_gpc()) { $content = stripslashes($content); }

$fullpage= "../" . $pagename . ".inc";
$file=fopen($fullpage,"w");
echo fwrite($file,$content);
fclose($fullpage);

echo "Some stuff to let user know the page has been updated.";

?>

penders

10:30 pm on Mar 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



THANK YOU SO MUCH, Penders! Your point about upper/lower case variables turned out to be the issue. It now works.

No worries, glad you got it sorted. Must admit I was assuming quite a bit regarding your GETs and POSTs - looks like you had that sorted already.

I tend to have my global vars all uppercase, hence "$CONTENT" - a global variable in my code.

Just a point... you could try setting:

error_reporting(E_ALL);

at least while developing, in order to enable E_NOTICE warnings (not enabled by default). This is likely to pick up errors such as the above. $content (lowercase) was probably an 'undefined variable' in this instance. Depending on your configuration you may also need to set:
ini_set('display_errors','1');

...oh, and welcome to webmasterworld! :)