Ignore PHP tags in Form Input?

Discussion in 'PHP' started by sweetfunny, Nov 8, 2008.

  1. #1
    This has got me stumped, i have a form field where a user can enter PHP code which is then echoed back out. It works if just the code is entered, but if they include the opening and closing PHP tags i get:

    Parse error: syntax error, unexpected T_VARIABLE

    I don't want to strip the tags from the output, but rather ignore them from processing and echo them back out if they are included in the forms input.

    An example of what i'm doing i guess would be Pastebin.com, users enters PHP and it's echoed back out with formatting and line numbering.

    Any ideas, thanks.
     
    sweetfunny, Nov 8, 2008 IP
  2. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Not sure if you have described it clearly, because PHP will 'interpret' tags as strings.
     
    rohan_shenoy, Nov 8, 2008 IP
  3. sweetfunny

    sweetfunny Banned

    Messages:
    5,743
    Likes Received:
    467
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Ok a description, a user pastes this in the form input:

    and it comes out:

    <?include("location.inc");?>
    PHP:
    With formatting, that's my desired result. But currently if the user pastes in the code with opening and closing PHP tags i get the error i mentioned above. If the user drops the opening/closing tags and just pastes the code it works fine.

    I want to be able to accept input with opening/closing PHP tags without throwing an error.

    Hope that's a bit clearer.
     
    sweetfunny, Nov 8, 2008 IP
  4. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #4
    If the example is Pastebin, i guess this works. (notice the htmlentities() )
    <?php
    // Read from form
    $user_code = $_POST['code'];
    
    // Echo it to user.
    echo 'Here is your code <br />', htmlentities($user_code);
    ?>
    PHP:
     
    xrvel, Nov 9, 2008 IP
  5. sweetfunny

    sweetfunny Banned

    Messages:
    5,743
    Likes Received:
    467
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi thanks for you help, i just tried htmlentities but unfortunately it had no effect. :(
     
    sweetfunny, Nov 9, 2008 IP
  6. dannywwww

    dannywwww Well-Known Member

    Messages:
    804
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    110
    #6
    You would use htmlentities...

    
    <?php
    /*
    Get code e.g. url.com/file.php?code=<?php echo "hello world!"; ?>
    or you'd change the $_GET to $_POST if using a form.
    */
    $code = htmlentities($_GET['code']);
    
    // echo the requested code
    echo "PHP Code:<br />".$code;
    ?>
    
    PHP:
     
    dannywwww, Nov 10, 2008 IP