0

Basically, I can write html tags/text etc etc to the file. All that is fine. However, I need to be able to write some actual php into the file and I'm completely oblivious of how I should go about doing this.

Anyway here is what I have got to help better demonstrate my situation:

$path = substr(md5(time() . md5(rand())), 0, 8);
$dir = substr(md5($_SERVER['REMOTE_ADDR']), 0, 4);
$fpath = "b/" . $dir "/" . $path;
$file = 'index.php';

$handle = fopen($fpath . '/' . $file, 'w') or die ('cannot create the file'); 
include template.php;
fwrite($handle, $pt1 . $pt2 . $pt3);

Inside the template.php I have variables holding a lot of html in $pt1,$pt2,$pt3 But, inbetween each 'pt' I need to have some actuall php.

So for example sake say I need $this inbetween each 'pt' and the $this being:

<?php 
if(isset($_SESSION['user'])){
echo "hello";
}else{
echo "bye";
} 
?>

How would I do it?

*The actual creating of the file and paths is fine though but, I know that it may look very unusual however, I like it that way. It's simply adding the php scripts to the php file.

  • also I have tried sticking the scripts inside variables but, it seems to just print them out as plain text in the file rather than executing them.
2
  • I guess you can use PHP's tokenizer, and insert that code 3 tokens after a T_VARIABLE named "pt" :) Why do you need this anyway? Commented Feb 17, 2013 at 20:07
  • Hmm, I will have a look into that as I've never heard of the tokenizer before. Though regarding the reason why* I'm doing this is because I need a template for the users saves, of which has to have php embeded within it. Commented Feb 17, 2013 at 20:10

1 Answer 1

1

Does inbetween each pt means $p1 . $var . $pt2?

Try something like this:

<?PHP
$var = '<?php 
if(isset($_SESSION["user"])){
echo "hello";
}else{
echo "bye";
} 
?>';
echo $pt1 . $var . $pt2;
?>

Edit:

Also, you can wrap long content into a variable using

<?PHP
$var = <<< 'PHP'
<?php 
if(isset($_SESSION["user"])){
echo "hello";
}else{
echo "bye";
} 
?>
PHP;
$pt1 = 'before';
$pt2 = 'after';
echo "$pt1 $var $pt2";
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, wait my bad. Putting them into variables again for the second time does work. I don't know what I did wrong the first time but thanks for getting my to give it another shot as it is executing the php now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.