dsbgltg159136540 2016-04-23 14:24
浏览 29

为什么在多次加载页面时会破坏PHP会话?

I have website where I am using login and logout functionality using php.

So, for login, first I call following function :

function sec_session_start() {
    $session_name = 'happiechef_session_ids';   // Set a custom session name
    $secure = false;
    // This stops JavaScript being able to access the session id.
    $httponly = true;
    // Forces sessions to only use cookies.
    if (ini_set('session.use_only_cookies', 1) === FALSE) {
        header("Location:index");
        exit();
    }
    // Gets current cookies params.
    $cookieParams = session_get_cookie_params();
    session_set_cookie_params($cookieParams["lifetime"],
        $cookieParams["path"], 
        $cookieParams["domain"], 
        $secure,
        $httponly);
    // Sets the session name to the one set above.
    session_name($session_name);
    session_start();            // Start the PHP session 
    session_regenerate_id(true);    // regenerated the session, delete the old one. 
}

and then I call following function to check user login information from mysql database :

function admin_login($email, $pass) {
    global $conn;   

    $query = mysqli_query($conn, "SELECT a_email, a_pass, a_id FROM admin_profile WHERE a_email = '$email' LIMIT 1");
    $query_result =  mysqli_fetch_array($query);
    $a_id = (int) $query_result['a_id']; 
    $db_hash = htmlspecialchars($query_result['a_pass']);
    $num = mysqli_num_rows($query);

    if($num == 1) {
        if (checkbrute($email) == true) {
        // if true account is locked
            return false;
        } else {
            if(verify($pass, $db_hash)) {
                $a_id = preg_replace("/[^0-9]+/", "", $a_id);
                $email = validate_data($email);
                $user_browser = $_SERVER['HTTP_USER_AGENT'];
                $_SESSION['logged_admin_user'] = $email;
                $_SESSION['logged_admin_id'] = $a_id;                
                $_SESSION['login_string'] = hash('sha512', $db_hash . $user_browser);
                return true;
            } else {
                $time = time();
                $query =  mysqli_query($conn, "INSERT INTO login_attempt VALUES('', '$email', '$time')");
                return false;
            }
        }
    } else {
        return false;
    }    
}

Well, when I refresh the page multiple time using F5 key from Keyboard it's automatically logged out and sometime when I visit other page it's asking me to login! Somehow it's destroyed the PHP session.

Can anyone tell me what is the problem in my code ?

Thanks In advance.

Update :

Here is the function to check if user is logged or not :

function admin_login_check() {
    // Check if all session variables are set 
    if (isset($_SESSION['logged_admin_user'], $_SESSION['logged_admin_id'], $_SESSION['login_string'])) {
        global $conn;
        $user_id = $_SESSION['logged_admin_id'];
        $login_string = $_SESSION['login_string'];
        $username = $_SESSION['logged_admin_user'];

        // Get the user-agent string of the user.
        $user_browser = $_SERVER['HTTP_USER_AGENT'];

        if($query = mysqli_query($conn, "SELECT a_pass FROM admin_profile WHERE a_email = '$username' ")) {
            $num =  mysqli_num_rows($query);
            if($num == 1) {

                $result =  mysqli_fetch_array($query);
                $password = htmlspecialchars($result['a_pass']);

                // if hash equals function is not exist    
                if(!function_exists('hash_equals')){
                    function hash_equals($str1, $str2){
                        if(strlen($str1) != strlen($str2)){
                            return false;
                        } else {
                            $res = $str1 ^ $str2;
                            $ret = 0;
                            for($i = strlen($res) - 1; $i >= 0; $i--) {
                                $ret |= ord($res[$i]);
                            }
                            return !$ret;
                        }
                    }
                }
                $login_check = hash('sha512', $password.$user_browser); 
                if (hash_equals($login_check, $login_string) ){                    
                    return true;
                } else {          
                    return false;
                }
            } else {
                 return false;
            }
        } else {
           return false;
        }
    } else {        
        return false;
    } 
}
  • 写回答

1条回答 默认 最新

  • dongyong8071 2016-04-23 14:31
    关注

    If you remove the session_regenerate_id(true) the session should not destroyed anymore.

    Why is this happening?
    session_regenerate_id() replace the current session ID with a new one. The session information will be kept. When you use this function to often (reload, AJAX, etc.) you can see this effect on your session. PHP has a restriction for access to the session for only one running task. If you run session_regenerate_id() to often / fast the task get into queue. So the following is happening:

    1. The first call changes the session ID and delete the old session (if parameter is true).
    2. The second call has still the old session ID and tries to do some operations on it.
    3. As the old session doesn't exists anymore, a new session would be created. The user is logged out now (session is invalid now).
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看