AJAX does not carry session variables
I have a centralized index page that contains all of my pages. Inside this index page, I have my session started. I tested this by echoing out all variables, it works.
On the AJAX php script, I try to call a session variable, and it seem to print anything out: not even a session id. According to my research, you need to call session_start() on the AJAX script. I did so, and it starts the session, but with a different ID and no access to session variables set on the main website. I have not found anything on the first 5 pages of Google replicating my problem.
IF I change the '".$_SESSION['usr']."' part of the mysqli query to my username, it works. This is also proof that it is, in fact, the AJAX script not seeing the session variables.
What can I do get the AJAX script to see the session that is on the main website and not it's own session with no data?
Code:
AJAX:
$(document).ready(function(){
// Moving North
$('#moveNorth').on('click', function(){
var value = $(this).text();
$.ajax({
url: "ajax/moveNorth.php",
type: "POST",
data: {data : value},
cache: false,
success: function(result) {
$('#coordinate-y').html(function(i, val) { return +val+1 });
},
error: function() {
alert("Something went wrong. Please email an admin.");
}
});
});
});
PHP Script:
<?php
$sessionTest = session_id();//$_SESSION['usr'];
echo "Session Test | " . $sessionTest . " |";
$mysqli = new mysqli("db_host", "db_user", "db_password", "db_name");
if($mysqli->connect_errno)
{
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
else
{
$mysqli->query("UPDATE table_name SET coord_x = coord_x - 1 WHERE username = '".$_SESSION['usr']."'");
}
?>
Page calling the AJAX:
<?php
$sessionTestID = session_id();//$_SESSION['usr'];
echo $sessionTestID;
?>
链接地址: http://www.djcxy.com/p/59724.html
上一篇: 单击链接时将会话变量传递到新页面
下一篇: AJAX不会携带会话变量