1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Getting back slashes when I generate a php file.

Discussion in 'PHP' started by blueparukia, Sep 30, 2007.

  1. #1
    Basically, I have an online text editor, and when you use PHP, before and after every ' or "you get a \.

    The way the process works is that you type in code into a textare, and when you submit it, it creates a .php file.

    The code used to genrate the file:

    
    <?php
    $filetype = $_POST['typeoffile'];
    $user = $_POST['username'];
    $filename = "".$user."".$filetype."";
    $host = $_SERVER['HTTP_HOST'];
    $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extra = $filename;
    $result = $_POST['code'];$filehandle = fopen($filename, 'w') or die("We have failed to open your file");
    fwrite ($filehandle, $result);
    chmod("$filename", 0777);
    $onlyconsonants = str_replace($slashes, "", "$host$uri/$extra");
    echo "Your file has been succesfully created. Click <a href='http://$host$uri/$extra'>here to view it.<br />"
    ?>
    PHP:
    My input into the textarea:

    <?php
    $ret = "hello";
    echo $ret;
    ?>
    PHP:
    The contents of the file after generation:
    <?php
    $ret = \"hello\";
    echo $ret;
    ?>
    PHP:
    How do I fix this, do I have an extra slasdh or something in PHP?

    Thanks,

    BP
     
    blueparukia, Sep 30, 2007 IP
  2. David26

    David26 Well-Known Member

    Messages:
    1,301
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    140
    #2
    Easy :)

    Just add:

    $ret = (stripslashes($ret)

    before:

    echo $ret;

    The added slash is there to prevent mysql entries to contain an apostrophe for hackers to ad sql injection. If this is php only and no db interaction you can just do what I posted.
     
    David26, Sep 30, 2007 IP
    blueparukia likes this.
  3. kkrizka

    kkrizka Peon

    Messages:
    223
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    David's suggestion is correct. Here is your code modified:

    
    <?php
    $filetype = $_POST['typeoffile'];
    $user = $_POST['username'];
    $filename = "".$user."".$filetype."";
    $host = $_SERVER['HTTP_HOST'];
    $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extra = $filename;
    $result = stripslashes($_POST['code']);
    $filehandle = fopen($filename, 'w') or die("We have failed to open your file");
    fwrite ($filehandle, $result);
    chmod("$filename", 0777);
    $onlyconsonants = str_replace($slashes, "", "$host$uri/$extra");
    echo "Your file has been succesfully created. Click <a href='http://$host$uri/$extra'>here to view it.<br />"
    ?>
    
    Code (php):
     
    kkrizka, Sep 30, 2007 IP
    blueparukia likes this.
  4. hemlata

    hemlata Peon

    Messages:
    18
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hello,

    EdIt :: Your problem is already solve. Please ignore this post

    Modify your code to following in order to get the required results.

    <?php
    $ret = 'hello, is it n\'t your "expected" solution?..';
    echo stripslashes($ret);
    ?>
    PHP:
    Hope this will solve your issue,
    Regards,
     
    hemlata, Sep 30, 2007 IP
  5. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #5
    Thanl you all. I just found the php.net tutorial on slashe.

    Thanks all, Rep Given
     
    blueparukia, Sep 30, 2007 IP