2017-05-30 81 views
0

我有一個index.html頁面,點擊一個鏈接沿着登錄表單滑動。
登錄按鈕的click事件對PHP腳本執行Ajax,成功時用戶將被重定向到另一個頁面(test.php)。jquery ajax無法創建php會話

問題是:我沒有得到任何錯誤。
瀏覽器創建一個PHPSESSID,頁面不會重定向。
直接瀏覽test.php結果$ _SESSION變量未定義錯誤。下面

代碼:

PHP登錄腳本:

<?php 
$servername = "localhost"; 
$username = "abcd"; 
$password = "12345"; 
$dbname = "abcdmaster"; 

$obj = new stdClass(); 
$obj->msg = ''; 
$obj->err = ''; 
$obj->uname = ''; 

// Create Database connection 
$conn = new mysqli($servername, $username, $password, $dbname); 

// Check connection 
if($conn->connect_error) { 
    die("connection failed: " . $conn->connect_error);   
} elseif(isset($_POST['userid']) and isset($_POST['password'])) { 
    session_start(); 
    $username = $_POST['userid']; 
    $password = $_POST['passwd'];  

    $sql = "select * FROM `adminuser` WHERE username=$username"; 

    $result = $conn->query($sql); 
    if($result->num_rows == 1) { 
    $row = $result->fetch_assoc(); 
    if(strcmp($row['password'],$password)==0) { 
     $obj->msg = "yes"; 
     $_SESSION['username'] = $row['username']; 
     $obj->uname = $_SESSION['username']; 
    } // end strcmp   
    } else { 
    $obj->msg = "no"; 
    //$obj->err = "Invalid Login Credentials"; 
    } 
    header('Content-Type: application/json'); 
    echo json_encode($obj); 
    return false;    
} // end isset 
?> 

的jQuery:

/* click event for login button */ 
$('.loginrow').on('click', 'input[value="Login"]', function() { 
    var creds = {} 
    creds.userid = $('#userid').val(); 
    creds.passwd = $('#password').val(); 
    console.log(creds); 
    $.ajax({ 
    url: 'http://localhost/server/adminlogin.php', 
    data: creds, 
    type: 'POST', 
    dataType: 'json',     
    }).done(function(data) { 
    if(data == 'yes') { 
     console.log("login ok"); 
     window.location.href = './server/test.php'; 
     } else { 
     console.log("login unsuccessful"); 
     } 
    });  
    return false; 
}); 

test.php的:

<?php 
session_start(); 
?> 
<!DOCTYPE html> 
<html> 
<body> 

    <?php 
    // Echo session variables that were set on previous page 
    echo "Logged on user is " . $_SESSION["username"] . ".<br>"; 

    ?> 

</body> 
</html> 

任何想法?

+0

添加'在session_start();'在adminlogin.php ...... –

+0

你必須在session_start添加到您的第一頁 – Icewine

+1

哎呀......你的代碼是脆弱的到SQL注入,你需要解決這個問題,另外,你需要以明文存儲密碼,這也是你需要修復的。 – Enstage

回答

0
  1. 請看看你的Ajax done

    .done(function(data) { 
        if(data == 'yes') { 
        console.log("login ok"); 
        window.location.href = './server/test.php'; 
        } else { 
        console.log("login unsuccessful"); 
        } 
    }); 
    

data是JSON對象這裏,所以(data == 'yes')永遠是假的。

您應該檢查(data.msg === 'yes')

  • 使用xhrFields: {withCredentials: true},在AJAX設置,以保持會話cookie:

    $.ajax({ 
        url: 'http://localhost/server/adminlogin.php', 
        data: creds, 
        type: 'POST', 
        dataType: 'json', 
        xhrFields: {withCredentials: true}, 
        ... 
    
  • +0

    感謝您的回覆,但沒有任何變化。 –

    0

    發現在adminlogin.php代碼一個錯字,線

    elseif(isset($_POST['userid']) and isset($_POST['password'])) 
    

    應該是

    elseif(isset($_POST['userid']) and isset($_POST['passwd'])) 
    

    如jQuery的阿賈克斯創造一個像對象:

    var creds = {} 
        creds.userid = $('#userid').val(); 
        creds.passwd = $('#password').val(); 
    
    +0

    非常感謝大家的幫助esp Kosh和Louys,正如您所建議的,也有助於解決這個問題。 –