-1

I am trying to generate a dynamic file with php. Within this code, I need to declare some $_SESSION variables. For instance, the redirect url on refresh etc.

However, when I try to declare the $_SESSION variable and use fwrite, it fails to generate the php file.

How can I make sure that I can?

My code:

<?php
header('Content-Type: text/plain; charset=utf-8');
$file = fopen("testfile.php","w");
echo fputs($file,"$_SESSION['test'] = 'Hello World. Testing!'");
fclose($file);
?>

Thanks for your help

  • You need to escape the `$_SESSION` string i.e. `"\$_SESSION = 'Hello world...'"` otherwise who knows what will happen. – apokryfos Mar 07 '16 at 16:22

1 Answers1

0

as apokryfos stated,

escaping the $_SESSION worked!

echo fputs($file,"$_SESSION['test'] = 'Hello World. Testing!'");

had to be modified to:

echo fputs($file,"\$_SESSION['test'] = 'Hello World. Testing!'");

Thanks!