dsadsadsa1231 2015-09-03 20:48
浏览 32

$ _SESSION变量未在AJAX调用中设置

I wrote this call for user authentication:

$( '.sign-in' ).click( function(e) {
    e.preventDefault();
    var logincall = $.ajax({
        type: "POST",
        url: "../snippets/auth.php",
        data: { username: $('#id_email').val(), password:   $('#id_password').val() }
    });
    logincall.done(function( result ) {
        alert( 'finished: ' + result );
        if( result == 'valid' ) {
            location.reload();
        } else {
            $( '.warning' ).toggleClass('hidden');
        }
    });
    logincall.fail(function( jqXHR, textStatus ) {
      alert( "Request failed: " + textStatus );
    });
});

and here's the auth.php function

if( isset($_POST['username']) && isset($_POST['password']) ) {

    $username = $_POST['username'];
    $password = md5($_POST['password']);
    $query = "SELECT COUNT(*) FROM member WHERE username='$username' AND password='$password';";
    $result = $mysqli->query($query);
    $result = $result->fetch_array(MYSQLI_ASSOC);

    if( $result['COUNT(*)'] !== 0 ) {
        $_SESSION['logged'] = true; //this sets
        $query = "SELECT id, level FROM member WHERE username='$username' AND password='$password';";
        $result = $mysqli->query($query);
        $result = $result->fetch_array(MYSQLI_ASSOC);
        $_SESSION['uid'] = $result['id']; //this DOES NOT set
        $_SESSION['lvl'] = $result['level']; //this DOES NOT set
        echo json_encode('valid');
    } else {
        echo json_encode('invalid');
    }
}

The odd thing is, if I navigate directly to

myurl.com/snippets/auth.php?username=myusername&password=mypassword

it DOES set the other two session variables, but I can never get index.php to pick them up.

I even added that refresh line to it to ensure it could pick them up, and it gets $_SESSION['logged'] just fine, but not ['uid'] or ['level']

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line