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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分