file_get_contents and setting cookies


file_get_contents and setting cookies

在PHP中,我使用file_get_contents向最终用户显示文件的内容,如:

$showfile = file_get_contents("http://website.org/dl.php?file=Filename");
echo ($showfile);

这通过dl.php程序,该程序获得文件名为$file,并包括下面的代码,如果在过去两个小时内没有设置文件名,则使用相同的文件名设置cookie。

setcookie($file, "set", time() + 60*60*2*1, "/", "website.org");

其他代码在cookie过期时增加下载计数器。当链接到文件名是一个普通的href链接时,它工作得很好,但当它使用file_get_contents显示时就不一样了。计数器在每次页面刷新时递增,显示dl.php正在使用该文件名被激活,但未能以正确的方式设置cookie,即使当我计算setcookie表达式时,它返回True

已尝试加载,但未能解决此问题。

尝试更改

setcookie($file, "set", time() + 60*60*2*1, "/", "website.org");

ob_start();
setcookie($file, "set", time() + 60*60*2*1, "/", "website.org");
ob_end_flush();