我在西湖1 2014-10-17 11:33
浏览 15
已采纳

PHP:记住我,保持登录状态不起作用

In my PHP project, I want to add a user remember me checkbox so that everybody can choose to stay logged in:

Until now I do my normal log in like:

public function loginUser($psMail, $psPwd, $pnRememberMe = 0) {
   // Check credentials and so on
   // If mail and password matches
   if(CREDENTIALS OKAY) {
      $_SESSION["username"] = "foo";
      $lnExpire = time() + 3600 * 24 * 60;
      setcookie("remember", base64_encode(USERID), $lnExpire);
      setcookie("rememberToken", md5(SOMESTUFF), $lnExpire);
   }
}

When I log in, I can see the created cookie variables with:

print_r($_COOKIE);

Now I try to leave the site with my logout function:

// Unset the session variables
$_SESSION = array();
// Destroy the session.
session_destroy();

But now, when I am at the landing page, there are also my cookies gone? Could this be because of my session site settings?

ini_set("session.use_only_cookies", "1");
ini_set("session.use_trans_sid", "0");
  • 写回答

2条回答 默认 最新

  • dongliangkeng1056 2014-10-17 11:40
    关注

    php function setcookie has fourth argument path, from documentation "The path on the server in which the cookie will be available on". By default it set path to actual your directory. Try set "/" Then it will be available for all domain. http://php.net/manual/en/function.setcookie.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大