【发布时间】:2023-03-27 06:47:01
【问题描述】:
我目前正在开发一个小网站,我必须在该网站上创建一个帐户创建系统。为了知道用户是否已经连接,我想使用 cookie 因为这看起来是最简单的方法。但是,在查看 php.net 和其他一些论坛上的官方文档后,我仍然无法创建我的 cookie。这是我的代码:
function connexionOK() {
$cookieName = "CookieCo";
$cookieValue = "true";
$isSent = setcookie($cookieName, $cookieValue, time() + 3600, "/");
if($isSent) {
echo '<script type="text/javascript">';
echo "alert('Cookie envoyé');";
echo "</script>";
}
else {
echo '<script type="text/javascript">';
echo "alert('Cookie failed');";
echo "</script>";
}}
所以页面确实在提醒我“Cookie 失败”。
感谢所有试图帮助我的人! :)
【问题讨论】:
-
来自手册:
If output exists prior to calling this function, setcookie() will fail and return FALSE. If setcookie() successfully runs, it will return TRUE. This does not indicate whether the user accepted the cookie.看起来你在调用 setCookie() 之前有输出 -
为我工作...您需要确保在发送任何输出之前调用 setcookie,也就是之前没有回声或打印
-
作为添加到约翰的评论,或者您也无法在同一页面上设置和检查。您必须重新加载页面(无需再次设置)或在其他页面上检查
-
另外,如果您需要保留现有的输出语句,您可以使用output buffering