dongqi7631 2012-05-09 17:19
浏览 15
已采纳

too long

i want to start a new session using a cookie, once the previous session has been destroyed.

if a cookie is set, but not a session, can i automatically, create a new session using only the cookie?

currently, i am implementing a remember me button. and part of my loggin code is

if($rememberme=="on"){
            $user_id=mysql_result($query_run,0,'id');
            setcookie("user_id",$user_id,time()+7200);
            $_SESSION['user_id']=$_COOKIE['user_id'];
            header('Location:front_page.php');
        }else if($rememberme==""){
            echo'ok';
            $user_id=mysql_result($query_run,0,'id');
            echo $user_id;
            $_SESSION['user_id']=$user_id;
            header('Location:front_page.php');

i need to do this because if a cookie is set, but not a session, it is causing various errors on my site. because i am using the $_SESSION['userid'] variable in various places. and if the session is not set, many of my functions are not working

edit: i tried the following and it did not work.

i want to start a new session using a cookie, once the previous session has been destroyed. any other suggestions?

set_session_from_cookie();

function set_session_from_cookie(){
if(isset($_COOKIE['user_id'])){
    $_SESSION['user_id']=$_COOKIE['user_id'];
}
}
  • 写回答

1条回答 默认 最新

  • doulu1914 2012-05-09 17:49
    关注

    I suggest you to use a different way for the "remember me" functionality.

    You can check if the "remember me" checkbox is checked then generate a UNIQUE and secret key and put the key inside a cookie and in some user row in database.

    Now, somewhere at top of pages, check for the cookie then if needed, set session.

    Example

    if(isset($_COOKIE["c_key"]) && !isset($_SESSION["userId"])) 
    { 
         /*
          * Here check for users matching $_COOKIE["c_key"] in database and get the userId
          */
         $_SESSION["userId"] = $user_data["userId"]; 
    }
    

    And keep using session everywhere.

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

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题