我正在創建一個網站,作爲主站點的子域www.login.example.com
www.example.com
。當(從子域),用戶登錄的用戶名和密碼由該AJAX腳本傳遞到主站點:PHP和Jquery Ajax不會啓動會話
<script>
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
function loginano() {
var div= document.getElementById('thes').value;
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
xmlhttp.open('POST', "https://example.com/sess.php", true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send('username='+username+'&password='+password);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var resp = JSON.parse(xmlhttp.responseText);
if(resp.status) {
div.innerHTML = "Great";
}else {
div.innerHTML = "Nope";
}
}else{
div.innerHTML = "Wait";
}
}
}
</script>
POST數據被接收和由該PHP代碼處理:
<?php
header("Access-Control-Allow-Origin: *");
session_start();
$servername = "localhost";
$username = "username";
$password = "pass";
$dbname = "dbname";
$loginusername = $_POST["username"];
$loginpassword = md5($_POST["password"]);
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql="SELECT * FROM tbl_users WHERE username='$loginusername' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
\t
while($row = $result->fetch_assoc()) {
$sessionid = $row['id'];
$sessionuser = $row['username'];
\t \t $userpassword = $row['password'];
\t
}}
\t if($loginpassword == $userpassword){
$_SESSION ["sessionid"]= $sessionid;
$_SESSION ["sessionuser"]= $sessionuser;
$return_arr["status"]=1;
} else {
\t $return_arr["status"]=0; \t
}
echo json_encode($return_arr); // return value
exit();
$conn->close();
?>
登錄作品它是成功的,但會議不會開始。我錯過了什麼或者可能是什麼問題?
你怎麼知道會話沒有開始?任何錯誤?之後你會在哪裏獲得'$ _SESSION'? – Jeff
它不啓動會話,因爲我寫了一個腳本來檢查會話,在這裏; –
'code' <?php session_start(); (isset($ _ SESSION [「sessionid」])){ echo $ _SESSION [「sessionid」]。「kk」; } else { echo「nope」; } ?>'code' –