2016-04-03 58 views
0

當用戶登錄時,我將用戶標識存儲到會話中,然後我想將該用戶標識傳遞給ajax以從數據庫中檢索其他信息。存儲在會話 用戶ID沒有被傳遞到allResult.php 我的登錄頁面的代碼段,其中i發出會話:如何使用存儲在會話中的用戶標識與ajax

$msg = ''; 

    if (isset($_POST['login']) && !empty($_POST['username']) 
    && !empty($_POST['password'])) 
    { 
    $username1 = $_POST['username']; 
    $password1 = $_POST['password']; 
    $password2 = md5($password1); 
    $sql= "SELECT * FROM users WHERE userName='$username1' AND password='$password2'"; 
    if($query_run = mysqli_query($conn, $sql)) 
    { 
    $query_num_rows = mysqli_num_rows($query_run); 
    $stmt =mysqli_prepare($conn, $sql); 
    mysqli_stmt_execute($stmt); 
    mysqli_stmt_store_result($stmt); 
    if($query_num_rows==1) 
    { 
    $row = mysqli_fetch_assoc($query_run); 
    $user_id = $row['id']; 
    $name=$row['Name']; 
     //creating session 

    $_SESSION['loggedin_time'] = time(); 
    $_SESSION['userid'] = $user_id; 
    $_SESSION['name'] = $name; 

    } 
} 
    else { 
     $msg = '*Wrong username or password'; 
    } 
} 

現在登錄後,用戶會被帶到儀表板:

<?php 
    session_start(); 
    include("functions.php"); 
    if(isset($_SESSION["userid"])) { 
    if(isLoginSessionExpired()) { 
     header("Location:loginPage.php"); 
    } 
    } 
    if($_SESSION['userid']=="" && $_SESSION['name']==""){ 
    header("location: loginPage.php"); 
    } 
    require 'dbconnect.php'; 
    $userId = $_SESSION['userid']; 
    echo "$userId"; 
    ?> 

在儀表板我試圖訪問用戶的其他數據與AJAX

 <script id="source" language="javascript" type="text/javascript"> 


      $(function() { 
      $("#tabs").tabs({active:0}); 
      $.ajax({          
       url: 'allResult.php', 
       method:"POST", 
       data: ({uid:'$userId'}),              
       dataType: 'json',      
       success: function(data)   
       { 

       var date= data[0]['date']; 
        var time= data[0]['time']; 
        var ip= data[0]['ip']; 
        var lux= data[0]['lux']; 
        var press= data[0]['press']; 
        var acc_x= data[0]['acc_x']; 
        var acc_y= data[0]['acc_y']; 
        var acc_z= data[0]['acc_z']; 



        $("#tabs-1").html("<b>date: </b>"+date+"<b> time: </b>"+time+"<b> ip: </b>"+ip+"<b> lux: </b>"+lux+"<b> press: </b>"+press+"<b> acc_x: </b>"+acc_x+"<b> acc_y: </b>"+acc_y+"<b> acc_z: </b>"+acc_z); 

      } , 
      error : function(request,error) 

      { 
       alert (error); 
       alert("Request: "+JSON.stringify(request)); 
      } 

      }); 
}); 

我需要獲得用戶ID爲了運行allResul t.php allResult.php

<?php 
ini_set('display_errors', 1); 
    ini_set('display_startup_errors', 1); 
    error_reporting(E_ALL); 
    require 'dbconnect.php'; 
    $user_id = $_POST["uid"] 
    $sql_query = "SELECT * FROM data where id like '$user_id';";   
    $result = mysqli_query($conn,$sql_query); 
    if(mysqli_num_rows($result) >0) 
    { 
    $row = mysqli_fetch_assoc($result); 

    $output [] = $row ; 

    echo json_encode($output); 
    } 
    else 
    { 
    echo "Error finding data.."; 
    } 

    ?> 
+0

所以最新的問題? – 2016-04-03 19:57:50

+0

不想讀取所有的代碼,但我認爲這可能有助於'<?php echo session_id();?>' –

+0

用戶標識未被傳遞給allResult.php – Android2841

回答

0

讓所有的用戶登錄數據被稱爲成JSON格式。

假設你已經全部數據在

somedomain.com/userData.php

<?php 
#userData.php 

if ($user == "loggedin") { 
    $data['name'] = $_SESSION[1]; //name 
    $data['email'] = $_SESSION[2]; //email 
    $data['extra'] = $_SESSION[3]; //etc. 
    echo json_encode($data, JSON_PREETY_PRINT); 
} 
?> 

然後得到使用jQuery的Ajax的數據爲實時。

-1

看起來像你缺少session_start()

+0

已經有用戶dasboard的會話啓動 – Android2841

+0

除了不正確,這個答案沒有詳細說明。如果你的答案會符合一行,你可能不應該發表。 – BotMaster

相關問題