我在西湖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条)

报告相同问题?

悬赏问题

  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路