Escape Characters: Printing PHP

Discussion in 'PHP' started by Jeremy Benson, Dec 10, 2013.

  1. #1
    Hello,

    I'm having a bit of trouble figuring out how to write php to a file. No matter how I escape it I get a slash before the closing > in the php tag..

    fwrite($profileFilePointer, "<?php require('./system/files/profile.php'); ?>" );
    PHP:
    I've tried

    fwrite($profileFilePointer, "<?php require('./system/files/profile.php'); ?/>" );
    PHP:
    and a few other ways of escaping, but nothing worked yet. lol.

    thanks.
     
    Jeremy Benson, Dec 10, 2013 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    That's weird - if I do this:

    
    <?php
    $file = fopen ('somefile.php','w');
    
    fwrite($file, "<?php require('./system/files/profile.php'); ?>" );
    
    ?>
    
    PHP:
    it outputs this (in somefile.php):
    
    <?php require('./system/files/profile.php'); ?>
    
    PHP:
     
    PoPSiCLe, Dec 10, 2013 IP
  3. hdewantara

    hdewantara Well-Known Member

    Messages:
    540
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #3
    hdewantara, Dec 10, 2013 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    There's no need to escape anything, really. All of the below works:
    
    <?php
    $file = fopen ('somefile.php','w');
    fwrite($file, "<?php require('./system/files/profile.php'); ?>" );
    fwrite($file, '<?php require("./system/files/profile.php"); ?>' );
    fwrite($file, '<?php require(\'./system/files/profile.php\'); ?>' );
    fwrite($file, '<?php require('.'\'./system/files/profile.php\''.'); ?>' );
    ?>
    
    PHP:
    Output:
    
    <?php require('./system/files/profile.php'); ?>
    <?php require("./system/files/profile.php"); ?>
    <?php require('./system/files/profile.php'); ?>
    <?php require('./system/files/profile.php'); ?>
    
    PHP:
     
    PoPSiCLe, Dec 10, 2013 IP
  5. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #5
    I think the problem is in some of the other code. I'll show the basic set up and what's happening. Not sure why, or how to fix it at the moment. I'm using expression web 4.
    
    <?php
    include('../system/scripts/phpclasses/systemclass.php');
    require('../system/prime_system_data/sqlpassword.php');
    //Some variables initiated here
    //some if statements and other code here...calls to database
    // Create User's Profile Page
     
      $profileFilePointer = fopen('../' . $_SESSION['userName'] . '.php','x');
     
      //Include The Base File
     
      fwrite($profileFilePointer, '<?php require(\'./system/files/profile.php\'); ?>'); // The closing php tag here is blue as if not it  quotes.
     
      //Close the files.
     
      fclose($profileFilePointer);
      }
      }
    ?>  // this closing bracket has a yellow pointy bracket with error: extra bracket 
    
    PHP:
     
    Last edited: Dec 11, 2013
    Jeremy Benson, Dec 11, 2013 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    You have two closing brackets, but since you're not showing the full file, no way to tell what's wrong. Try removing the bracket?
     
    PoPSiCLe, Dec 12, 2013 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    Try this:
    
    <?php
    fwrite($profileFilePointer, '<' . '?php require(\'./system/files/profile.php\'); ?' . '>');
    ?>
    
    PHP:
    And are you sure "x" is the best mode for your situation in your fopen() call?
     
    nico_swd, Dec 12, 2013 IP
  8. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #8
    That looks like a good fix :) I thought that first bracket in the write function was closing off the php block, lol. Thanks :)

    I think x is, because the file will only be created once. I'm using the file being included as a base file for all profiles... There's probably no need for it to be x over some other method as it's only happening once...but I figured that would ensure that it wouldn't overwrite the profile on a second call...lol...seems like redundant security since I'm only writing to the file once, but what ever? lol.
     
    Jeremy Benson, Dec 12, 2013 IP