Search code examples
phpsessiondynamicfwritedeclare

PHP writing dynamic files with $_SESSION declaration fails


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


Solution

  • 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!