使用$ _SESSION声明编写动态文件的PHP失败

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

drr25281
drr25281 你需要转义$_SESSION字符串,即“\$_SESSION='Helloworld...'”,否则谁知道会发生什么。
4 年多之前 回复

1个回答



as apokryfos </ strong>声明,</ p>

转义$ _SESSION工作! </ p>

  echo fputs($ file,“$ _ SESSION ['test'] ='Hello World。Testing!'”); 
</ code> </ pre> \ n

必须修改为:</ p>

  echo fputs($ file,“\ $ _ SESSION ['test'] ='Hello World.Testing!'”  ); 
</ code> </ pre>

谢谢!</ p>
</ div>

展开原文

原文

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!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐