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


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

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

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'];

整个页面实际上消失了,没有任何代码有效。 我应该转义部分$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

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