douyinyi7766 2013-04-29 12:01
浏览 49
已采纳

PHP会话变量意外清除,尽管明显匹配,db值检查为空

I have been struggling with this one for hours and hours and just cannot figure out what I'm missing.

I'm trying to build a cookie-less login form that also has no information in session variables that would harm the app if an attacker would be able to modify them.

All of my pages have the below code included.

I have 2 issues:

  1. Every time I click on another page it acts like $_SESSION['token'] was empty and goes to the login page like if it was the first visit.

  2. It returns $tokenid and $tokentype empty however I'm calling them both every time a page is loading (aiming to avoid having to put them into a session variable).

This is my current code:

<?php

define('TIMEOUTMIN', 15);

define('LOCKOUTMIN', 10);

define('LOCKOUTNUM', 3);

include("includes/pp/pbkdf2.php"); // this is basically calling the validate_password function

include ("includes/vars/vars_dbconn.php"); // this contains the db data and $pdo

$userid = $_POST['userid'];
$userpw = $_POST['password'];

$deltoq = "UPDATE LoginUser SET token = ?, online = ? WHERE online < ?";
$prepdeltoq = $pdo->prepare($deltoq);
$prepdeltoq->execute(array(NULL,NULL,time()));

$loginq = "SELECT * FROM LoginUser WHERE ID = ?";
$preplq = $pdo->prepare($loginq);
$preplq->execute(array($userid));
$getuser = $preplq->fetch(PDO::FETCH_ASSOC);
$dbid = $getuser['ID'];
$dbpass = $getuser['hash'];
$dbbp = $getuser['bp'];
$dbltime = $getuser['ltimeout'];

$logintoq = "SELECT * FROM LoginUser WHERE token = ?";
$prepltq = $pdo->prepare($logintoq);        
$prepltq->execute(array($_SESSION['token']));
$getoken = $prepltq->fetch(PDO::FETCH_ASSOC);
$tokenid = $getoken['ID'];
$tokentype = $getoken['type'];
$totoken = $getoken['token'];

$prolonglock = $pdo->prepare("UPDATE LoginUser SET ltimeout = ? WHERE ID = ?");
$addbp = $pdo->prepare("UPDATE LoginUser SET bp = ? WHERE ID = ?");
$loginwhen = $pdo->prepare("UPDATE LoginUser SET lastlogin = ? WHERE ID = ?");
$loginlogq = $pdo->prepare("INSERT INTO LoginUserLog (ID, action)       
    VALUES(:ID, :action)");

$logintokenid = $pdo->prepare("UPDATE LoginUser SET token = ? WHERE ID = ?");
$loginonid = $pdo->prepare("UPDATE LoginUser SET online = ? WHERE ID = ?");
$loginontok = $pdo->prepare("UPDATE LoginUser SET online = ? WHERE token = ?");

if(!function_exists('LoginUser')) {

    function LoginUser($pwmessage) {

        if (session_name() <> 'MyWebApp') session_name('WesoftskyLogin');
        if (!session_id()) session_start();

        $_SESSION['token'] = '';

        include ("includes/header.php"); ?>
        <meta name="description" content="Login - MyWebApp"/>
        <title>Login - MyWebApp</title>
        <script type="text/javascript">
            event.keyCode == '';
                function enterTab() {
                    if (event.keyCode == 13) {
                    var passInput = document.getElementById("password");
                    passInput.focus();
                    }
                }
        </script>
    </head>
    <body onkeyup="enterTab()">
      <div id="homewrap">
        <div id="hometitle">MyWebApp</div>
      </div>
      <div id="id_formwrap">
        <form action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']); ?>" method="post">
            <?php if (empty($pwmessage)) echo '<div>Please enter your login details</div>'; else echo '<div style="color:red">'.$pwmessage.'</div>'; ?><br />
            Login ID<br />
            <input type="text" name="userid" id="id" onKeyPress="return noenter(event)" /><br /><br />            
            <script>document.getElementById("id").focus()</script>
            Password<br />
            <input type="password" name="password" id="password" /><br /><br />
            <input type="submit" name="login" id="Submit" value="Login" />
        </form>
      </div>
    </body>
    </html>

    <?php exit();
    }
}

if(!function_exists('ProlongTime')) {
    function ProlongTime() {
        global $userid; 
        global $logintokenid;
        global $loginonid;
        global $loginontok;
        $timeoutodb = (time () + TIMEOUTMIN*60);
        if (!empty($userid)) {
            $_SESSION['token'] = bin2hex(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM));
            $logintokenid->execute(array($_SESSION['token'], $userid));
            $loginonid->execute(array($timeoutodb, $userid));
        } else {
            $loginontok->execute(array($timeoutodb, $_SESSION['token']));
        }
    }
}

if ($dbltime > time()) {
    $lockcheck = time() + LOCKOUTMIN*60;
    $prolonglock->execute(array($lockcheck,$userid));
    LoginUser('Your account is currently locked');
}

if(isset($_POST['logout'])) {
    $action = "Logged OUT";
    $loginlogq->execute(array(':ID' => $tokenid, ':action' => $action));
    LoginUser('Logged out');
}

if (isset($_POST['login'])) {   
    if ($dbid AND validate_password($userpw, $dbpass)) {  // Good login info
        //session_regenerate_id(true);      
        $action = "Logged IN";
        $loginlogq->execute(array(':ID' => $userid, ':action' => $action));
        $loginwhen->execute(array(time(), $userid));
        $addbp->execute(array(NULL, $userid));
        ProlongTime();
    } else { // Bad login info
        if ($dbbp >= LOCKOUTNUM-1) {
            $lockbp = time() + LOCKOUTMIN*60;
            $prolonglock->execute(array($lockbp,$userid));
            $action = "Locked (wrong password)";
            $loginlogq->execute(array(':ID' => $userid, ':action' => $action));
            LoginUser('Your account has been locked');
        }
        $addbp->execute(array($dbbp+1, $userid));
        $action = "Failed login";
        $loginlogq->execute(array(':ID' => $userid, ':action' => $action));
        LoginUser('Username or password is incorrect');
    }
} elseif (empty($_SESSION['token'])) { // Loading the page first time (new session)
    LoginUser('');
} elseif ($_SESSION['token'] <> $totoken) { // Session timeout
    $action = "Logged OUT (expired)";
    $loginlogq->execute(array(':ID' => $tokenid, ':action' => $action));
    echo 'tokenid: '.$tokenid;
} else ProlongTime(); // While using the app and still within time


$pdo = null;

?>
  • 写回答

1条回答 默认 最新

  • dongyejun1983 2013-04-29 12:04
    关注

    You need to put

    session_start()
    

    in the starting of the page.

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

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?