doudu2591 2013-09-25 20:23
浏览 7
已采纳

记住用户好几天 - 饼干

I am trying to set a cookie to remember users (for a period of 3 days) so they will not have to logon to the website everytime they close their browser.

I've tried the following:

<?php
 if(isset($_POST["logind_ok_tjek"]))
 {
     if ($stmt = $this->mysqli->prepare("SELECT `id`, `rank_hold`, `navn`, `efternavn`, `email_indhold`, `adgangskode`, `rank`, `img` FROM `bruger` WHERE `email_indhold` = ? or tlf = ? AND `adgangskode` = ?"))
     {
         $stmt->bind_param('sss', $email_indhold, $tlf, $adgangskode);
         $email_indhold = $_POST["email"];
         $tlf = $_POST["email"];
         $adgangskode = sha1($_POST["pass"]);


         $stmt->execute();
         $stmt->store_result();
         $stmt->bind_result($id, $rank_hold, $navn, $efternavn, $email_indhold, $adgangskode, $rank, $img);
         $stmt->fetch();
         $count = $stmt->num_rows;
         $stmt->close();

         if($count > 0)
         {
             $_SESSION["logged_in"] = true;
             $_SESSION["id"] = $id;
             $_seesion["rank_hold"] = $rank_hold;
             $_SESSION["navn"] = $navn . " " . $efternavn;
             $_SESSION["rank"] = $rank;
             $_SESSION["img"] = $img;
             $_SESSION["mail"] = $email_indhold;

             setcookie("Navn", $navn . " " . $efternavn, time()+3600);
             setcookie("indhold", $rank_hold, time()+3600);


                 if($_SESSION["logged_in"] == true)
                 {
                     if ($stmt = $this->mysqli->prepare('UPDATE `bruger` SET `online_sidste`=? WHERE `id`=?')) {
                         $stmt->bind_param('si', $online_sidste, $id);
                         $online_sidste = date('Y-m-d H:i:s');
                         $id = $_SESSION["id"];
                         $stmt->execute();
                         echo "Log ind nu!!";
                         $stmt->close();

                     } else {
                         echo 'Der opstod en fejl i erklæringen: ' . $mysqli->error;
                     }                    
                 }
                 else
                 {
                     echo "Fejl..";    
                 }
         }
         else
         {
             echo "Forkert Email eller password.";
         }
     }
     }
     ?>

To access the cookie, I am using:

<?php
         echo $_COOKIE["Navn"];
         ?>

This is not working, however...

  • 写回答

1条回答 默认 最新

  • dsj0312 2013-09-25 20:34
    关注

    // set a cookie that expires in 72 hours,you can try with this

    setcookie("Navn", $navn . " " . $efternavn, time()+3600*72);
    setcookie("indhold", $rank_hold, time()+3600*72);
    

    setcookie function expire parameter takes the value in seconds.time() will output the current unix_timestamp, then you add 3600*72 (3600 seconds * 72 hours) which is equivalent to 3 days (in seconds).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写