tgavin Posted February 8, 2008 Share Posted February 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/90093-solved-convert-to-string/ Share on other sites More sharing options...
cooldude832 Posted February 8, 2008 Share Posted February 8, 2008 How are u trying to input it if its quoted properly no issues Quote Link to comment https://forums.phpfreaks.com/topic/90093-solved-convert-to-string/#findComment-461909 Share on other sites More sharing options...
laffin Posted February 8, 2008 Share Posted February 8, 2008 use backslashes in front of the variables $str="<?php require_once(\$_SESSION['site_path'].'/data/config.php'); ?>"; Quote Link to comment https://forums.phpfreaks.com/topic/90093-solved-convert-to-string/#findComment-461910 Share on other sites More sharing options...
tgavin Posted February 8, 2008 Author Share Posted February 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/90093-solved-convert-to-string/#findComment-461919 Share on other sites More sharing options...
akitchin Posted February 8, 2008 Share Posted February 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/90093-solved-convert-to-string/#findComment-461932 Share on other sites More sharing options...
kenrbnsn Posted February 8, 2008 Share Posted February 8, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/90093-solved-convert-to-string/#findComment-461940 Share on other sites More sharing options...
tgavin Posted February 8, 2008 Author Share Posted February 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/90093-solved-convert-to-string/#findComment-461950 Share on other sites More sharing options...
akitchin Posted February 8, 2008 Share Posted February 8, 2008 ken should still have it right - i think you only need to escape the single quotes, and the delimiters take care of the rest. Quote Link to comment https://forums.phpfreaks.com/topic/90093-solved-convert-to-string/#findComment-462037 Share on other sites More sharing options...
kenrbnsn Posted February 8, 2008 Share Posted February 8, 2008 I tested my code snippet and the resultant file contained the string, so it works. Ken Quote Link to comment https://forums.phpfreaks.com/topic/90093-solved-convert-to-string/#findComment-462055 Share on other sites More sharing options...
tgavin Posted February 8, 2008 Author Share Posted February 8, 2008 Thanks guys for your help. It turned out to be a combination of the slashes and moving the code to another script (that didn't have output buffering!) Quote Link to comment https://forums.phpfreaks.com/topic/90093-solved-convert-to-string/#findComment-462098 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.