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
https://forums.phpfreaks.com/topic/90093-solved-convert-to-string/
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.

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.

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.