为什么“$_COOKIE”导致我的 fwrite() 函数不起作用?

Why is "$_COOKIE" causing my fwrite() function to not work?

我有一个 fwrite() 函数来创建电子邮件激活 link。我需要为此使用 cookie link。首先我输入:

if ($filecheck == "F") {
$file = $getuseremailresult['username'] . rand() . rand() . ".php";
$filename = "../" . $file;
$link = "http://new.connectionsocial.com/users/LoginRegisterName/" . $file;
$handle = fopen($file, "w") or die("<h1 style='text-align: center; color: red;'>There has been an error creating the activation link. Please try again later.</h1>");
$content = "

include_once('ConnectionDB.php');

$cookie = sha1($_COOKIE['TemporaryCookie']);

?>
";
fwrite($handle, $content);
mysqli_query($connection, "UPDATE userfilescheck SET FileCreated='T'");
}

到目前为止一切都很好。但是,当我将其添加到 $content:

$cookie = sha1($_COOKIE['TemporaryCookie'];

整个页面完全消失了,none 代码有效。我应该转义部分 $cookie 值,还是只是一个解析错误?

我不知道这是否是全部问题,但是...

$cookie = sha1($_COOKIE['TemporaryCookie'];

...缺少右括号。应该是...

$cookie = sha1($_COOKIE['TemporaryCookie']);

正在将我的评论转换为答案。

首先在 $content 外声明 $cookie = sha1($_COOKIE['TemporaryCookie']);,同时确保 cookie 已经创建并存在,然后在 $content 内做 $cookie=$cookie;

您可以尝试的另一件事是 heredoc 或 nowdoc http://php.net/manual/en/language.types.string.php

正如你在评论中提到的,我确实想到但在评论中没有提到的是用黑斜杠转义变量。