1

I'm trying this:

<a href="
<?php
$fl= $p['n'].".php";
$fh = fopen($fl, 'w') or die("Can't create file");
if($fh)
{
$code ="
<?php
// html and php combined code (the php is for sessions)
?>";
echo fwrite($file,$code); 
    fclose($file); 
}?>"></a>

The problem is that the codes inside $code are being evaluated and I need that $code simply save the codes like a string.

1
  • 1
    You probably want to quote that file name. Commented Mar 20, 2018 at 16:34

2 Answers 2

2

Use PHP's nowdoc feature (emphasis mine):

Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping.

Example

<?php

$test_var = 1;

$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
$test_var
EOD;

echo $str;

Outputs

Example of string
spanning multiple lines
using nowdoc syntax.
$test_var

So this ...

<a href="
<?php
$fh = fopen($p['n'].'.php', 'w') or die("Can't create file");
if($fh)
{
$code = <<<'EOD'
<?php
// html and php combined code (the php is for sessions)
?>"
EOD;
echo fwrite($file,$code); 
fclose($file); 
}?>"></a>

... should work for you.

Sign up to request clarification or add additional context in comments.

Comments

1

Using "$foo" will evaluate, but '$foo' will not.

However, this is opens massive security risks so I would probably back up and take a look at what you’re trying to do.

Comments

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.