dongzhuandian3292 2014-12-18 11:21
浏览 13
已采纳

PHP - 会话不匹配,不同的id和错误的用户,劫持风险和主要的安全风险

Found a major problem on my website. I found tha if I login with user A. it sometimes kinda does log in but actually doesn't. Then I login with user B -> enter the site. I log out and then go manually back to url where login is needed and it somehow goes in with user A. It seems that I have two (maybe could have more) session_id cookies on different tabs or there is a ghost session_id that comes active I don't know. Pulling my hairs here.

Also found that, lets say I have a user dashboard and test page. With a little going back and forth with different credentials. I get this result: Dashboard echoes user A's id, test echoes user B's id or not id at all. What the heck I am doing wrong with my sessions? Login is done with AJAX. Login validation is the same on every page.

COMMON FUNCTIONS:

    function validateUser($userid) {
    session_regenerate_id();
    $_SESSION['valid'] = 1;
    $_SESSION['usersid'] = $userid;
}

function isLoggedIn() {
    if (isset($_SESSION['valid']) && $_SESSION['valid'] == 1) {
        return true;
    } else {
        return false;
    }
}

function logout() {
    $_SESSION = array();
    session_unset();
    session_destroy();
}

LOGIN/DB: Login page:

    session_start();
include 'include_files.php';

if(isLoggedIn()){
    header('Location:loginrequiredpage.php');
    die();
}

Login page sends username/password with AJAX to an controller php file that uses db functions as included file. It executes usercheckfunc() which checks user from db and then echoes succes or fail back to ajax.

from db functions - part of user check function

//if user found from db and password hash match
validateUser(**ID FROM DATABASE**);

Back in login page if ajax gets success message back, JS send user to login required url. Here's where mystery sometimes occur The browser acts like if i just logged in somewhere, but the login page is loaded again. Sometimes I can manually go to login required page via address bar. Sometimes if I logout/idle too long etc. and login with different username/password I get in as a wrong user. Entered as user A, See user B's data OR echo different userids on pages or echo id only on other page.

LOGIN REQUIRED PAGE:

    <?php
    session_start();
    require_once 'include_files.php';
    if (!isLoggedIn()) {
        logout();
        header('Location:login.php');
        die();
    }

echo $_SESSION['usersid'];

Test page:

    <?php
    session_start();
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    require_once 'include_files.php';
    if (!isLoggedIn()) {
        logout();
        header('Location:login.php');
        die();
    }
echo $_SESSION['usersid'];

Is there a "best" way to manage sessions? Help is much appreciated :)

  • 写回答

1条回答 默认 最新

  • 普通网友 2014-12-30 08:58
    关注

    Got rid of the problem by manually setting session cookie parameters everywhere before session_start is executed. Now the session cookie domain doesn't behave unexpectedly. Sorry, no idea why it did that mysterious changeing before.

    This cookie parameters sets it to be valid on whole domain. I guess it's no good in situation where you need different sessions on the same domain (different applications etc.). But for me it was the healing patch I needed.

    session_set_cookie_params(0, '/', '.example.com');
    session_start();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图