Results 1 to 3 of 3

Thread: [resolved] PHP escaping my strings

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    121

    Resolved [resolved] PHP escaping my strings

    Hello all, I have a form page which submits to a PHP page. The latter creates a text file, but is escaping my strings for some reason.


    Here is my form page:

    Code:
    <form name="frmMain" action="write.php" method="post">
    <textarea name="txtBody"></textarea>
    <input type="submit">
    </form>
    Here is my processing page:

    Code:
    <?php
    $filename = "output.txt";
    $fp = fopen ($filename,"w") or die("Can't open file.");
    	fwrite ($fp, $txtBody);
    fclose ($fp);
    ?>
    Now when I submit the following text:

    bgcolor="#000000"

    It comes out as:

    bgcolor=\"#000000\"


    Why is PHP escaping my strings? More importantly, how do I stop it?
    Thanks!
    Last edited by solitario; May 27th, 2005 at 02:56 PM.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    PHP has feature called magic quotes. When activated all variables submitted to the PHP script via POST, GET or cookies, have meta characters (", ' and \) escaped automatically. Unfortunatley unless you have access to the PHP.ini file you cannot disable or enable magic quotes.

    Therefore, PHP provides three functions that enable you to tell whether or not the string has been escaped by default:

    get_magic_quotes_gpc() - returns 1 if magic quotes is on. Indicating meta characters in submitted data have been automatically escaped. If it returns 0 then magic quotes is disabled.

    addslashes() - adds back slashes to the string supplied as an argument and returns the escaped string

    stripslashes() - removes back slashes from the string supplied as an argument and returns the unescaped string.

    So to remove these slashes added automatically you can use the following code:
    PHP Code:
    if (get_magic_quotes_gpc ()) // slahses added automatically
        
    $filename stripslashes ($filename); // remove them 
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    121
    Worked like a charm. Thanks visualAd.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width