Jump to content

[SOLVED] convert <?php $foo ?> to string


tgavin

Recommended Posts

In a script I need to write some code to a page, so that when that page is accessed in a browser the code executes. Basically, take the following code, convert it to a string and then write it to file. My problem is that instead of being converted to a string and written to file, it's parsed.

 

<?php require_once($_SESSION['site_path'].'/data/config.php'); ?>

 

I've looked at eval() and string on the php site but haven't found an answer.

 

Link to comment
Share on other sites

I've tried backslashes, etc. until blue in the fingers.

I posted the code without slashes in the hopes that somebody could give me an example.

 

The only thing I can think of at this point is to put the code into a .txt file and include it. I know it works, but it's not as elegant because it's just another file on the server to keep track of.

Link to comment
Share on other sites

give something like this a whirl:

 

$command = '\<\?php require_once($_SESSION[\'site_path\'].\'/data/config.php\'); \?\>';

 

echo $command and see what it spits out.  by using single quotes (which tells PHP not to interpolate anything) and escaping the appropriate characters, you be able to avoid parsing.

Link to comment
Share on other sites

I think you're asking how to write the string

 

<?php require_once($_SESSION['site_path'].'/data/config.php'); ?>

 

to a file. Correct?

 

Try

<?php
$fp = fopen('yourfile.here','w');
fwrite($fp,'<?php require_once($_SESSION[\'site_path\'].\'/data/config.php\'); ?>'."\n");
fclose($fp);
?>

 

Ken

Link to comment
Share on other sites

akitchen

this is what's returned: \<\?php require_once($_SESSION['site_path'].'/data/config.php'); \?\>. This is what I've been struggling with! :) I tried echo stripslashes($command) but, of course, it just parsed instead.

 

kenrbnsn

Yes, I'm writing this to a file. However, this is just a snippet of the entire contents of what I'm writing to the file.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.