Php Ajax调用无意中破坏会话数据
我正在会话数据中存储有关用户的信息。我的一个网页,我们称之为Php Ajax调用无意中破坏会话数据,php,ajax,session,datatables,Php,Ajax,Session,Datatables,我正在会话数据中存储有关用户的信息。我的一个网页,我们称之为table.php是一个PIE表(使用datatables)table.php正在使用datatables ajax服务器端功能,并正在访问名为serverside.php的脚本。现在,为了确保允许用户查看该表,table.php和serverside.php启动会话并检查用户变量。这是可行的,但是table.php中每15次刷新中就有1次会导致删除所有会话数据。为什么会这样 同样的会话代码也适用于其他ajax脚本。但是桌子把一切都搞砸
table.php
是一个PIE表(使用datatables)table.php
正在使用datatables ajax服务器端功能,并正在访问名为serverside.php
的脚本。现在,为了确保允许用户查看该表,table.php
和serverside.php
启动会话并检查用户变量。这是可行的,但是table.php
中每15次刷新中就有1次会导致删除所有会话数据。为什么会这样
同样的会话代码也适用于其他ajax脚本。但是桌子把一切都搞砸了
这可能是因为ajax脚本几乎与table.php
同时被调用吗
init和dinit以及下面的代码段仅用于实例化和稍后取消设置无法序列化的变量(如PDO)。dinit还包含会话写入关闭()
Table.php如下所示:
<?php
Define("EXEC",true);
include('/var/www/private/core/AutoLoader.php');
\helpers\SessionManager::start('A Name');
$_SESSION['User']->init();
if ($_SESSION['User']->getPermissions() > 1) {
$_SESSION['User']->dinit();
die("You must be an admin to access this site");
}
?>
<table id="items" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>View</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>View</th>
</tr>
</tfoot>
</table>
<script type="text/javascript">
$(document).ready(function() {
$("#items").dataTable({
"processing": true,
"serverSide": true,
"ajax": "/serverside.php",
"order": [[ 0, "desc" ]],
"lengthMenu": [[5, 10, 15, 20, -1], [5, 10, 15, 20, "All"]],
"displayLength": 10,
"stateSave": true,
"createdRow": function (row, data, dataIndex) {
if (data[3] == 1) {
$(row).addClass("enabled");
} else {
$(row).addClass("disabled");
}
}
});
});
</script>
<?php
$_SESSION['User']->dinit();
?>
<?php
define("EXEC", true);
require_once('/var/www/private/core/AutoLoader.php');
\helpers\SessionManager::start('A Name');
$_SESSION['User']->init();
if ($_SESSION['User']->getPermissions() > 1) {
$_SESSION['User']->dinit();
die("You must be an admin to access this site");
}
class SSP {
public static function simple(PARAMTERS)
This function works, a bunch of code here
}
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
$_SESSION['User']->dinit();
?>
身份证件
名称
描述
看法
身份证件
名称
描述
看法
$(文档).ready(函数(){
$(“#项”)。数据表({
“处理”:对,
“服务器端”:正确,
“ajax”:“/serverside.php”,
“订单”:[[0,“说明”]],
“长度菜单”:[[5,10,15,20,-1],[5,10,15,20,“全部”],
“显示长度”:10,
“国家拯救”:没错,
“createdRow”:函数(行、数据、数据索引){
如果(数据[3]==1){
$(行).addClass(“已启用”);
}否则{
$(行).addClass(“已禁用”);
}
}
});
});
ServerSide.php看起来像这样:
<?php
Define("EXEC",true);
include('/var/www/private/core/AutoLoader.php');
\helpers\SessionManager::start('A Name');
$_SESSION['User']->init();
if ($_SESSION['User']->getPermissions() > 1) {
$_SESSION['User']->dinit();
die("You must be an admin to access this site");
}
?>
<table id="items" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>View</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>View</th>
</tr>
</tfoot>
</table>
<script type="text/javascript">
$(document).ready(function() {
$("#items").dataTable({
"processing": true,
"serverSide": true,
"ajax": "/serverside.php",
"order": [[ 0, "desc" ]],
"lengthMenu": [[5, 10, 15, 20, -1], [5, 10, 15, 20, "All"]],
"displayLength": 10,
"stateSave": true,
"createdRow": function (row, data, dataIndex) {
if (data[3] == 1) {
$(row).addClass("enabled");
} else {
$(row).addClass("disabled");
}
}
});
});
</script>
<?php
$_SESSION['User']->dinit();
?>
<?php
define("EXEC", true);
require_once('/var/www/private/core/AutoLoader.php');
\helpers\SessionManager::start('A Name');
$_SESSION['User']->init();
if ($_SESSION['User']->getPermissions() > 1) {
$_SESSION['User']->dinit();
die("You must be an admin to access this site");
}
class SSP {
public static function simple(PARAMTERS)
This function works, a bunch of code here
}
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
$_SESSION['User']->dinit();
?>
SessionManager类
<?php
namespace helpers;
use \library\User;
/**
* Session Manager to ensure security and make easy functions
* @author Help from http://blog.teamtreehouse.com/how-to-create-bulletproof-sessions
**/
class SessionManager {
/**
* Session start function
* @param $limit time to maintain session
* @param $path the path under the domain for which it should work
* @param $secure SLL override
**/
public static function start($name,$limit = 0, $path = '/', $secure = null) {
// Run session_start() Wrapper
self::sessionStart($name,$limit,$path,$secure);
if (!self::isSessionStarted()) {
echo "NEW";
self::destroySession();
self::sessionStart($name,$limit,$path,$secure);
self::createNewUser();
} else if (rand(1,100) <= 5) {
session_regenerate_id();
}
//print_r($_SESSION);
}
/**
* Create the new session for this user
**/
private static function createNewUser() {
$_SESSION = [];
$_SESSION['User'] = new \library\User($_SERVER['REMOTE_ADDR'],$_SERVER['HTTP_USER_AGENT']);
}
/**
* Session Start Wrapper to define cookie paramters
* @param $limit time to maintain session
* @param $path the path under the domain for which it should work
* @param $secure SLL override
**/
private static function sessionStart($name,$limit = 0, $path = '/', $secure = null) {
// Set the name of the Session
session_name($name);
// Set domain name for cookie to work under
$domain = $_SERVER['SERVER_NAME'];
//Parent Domain Modification
$domain = explode(".", $domain);
$domain = "." . $domain[count($domain) - 2] . "." . $domain[count($domain) - 1];
// Set the default secure value to whether the site is being accessed with SSL
$https = isset($secure) ? $secure : isset($_SERVER['HTTPS']);
// Set the cookie settings and start the session
session_set_cookie_params($limit, $path, $domain, $https, true);
session_start();
}
/**
* Destroy the session
**/
public static function destroySession() {
$_SESSION[] = [];
session_destroy();
}
/**
* @return Boolean value of it the session has proper variables
**/
private static function isSessionStarted() {
return isset($_SESSION['User']) && $_SESSION['User'] instanceof User;
}
}
?>
编辑:添加async:false
没有帮助。请显示示例代码。我们无法通过在您的serverside.php
和table.php
中输入代码来回答您的问题。这两个文件可能有一个可能导致删除您的session@RomnickSusa是的,你是对的,我会编辑我的帖子。我只是想看看这是否是一个已知的问题。@RomnickSusa我希望这不太容易筛选,如果你有任何疑问,请告诉我。如果你将async:false
(或者无论现在同步调用的标准是什么)设置到ajax,你仍然会丢失会话数据吗?@Andrew不确定你的意思。如何/在何处将其设置为false?请显示您的示例代码。我们无法通过在您的serverside.php
和table.php
中输入代码来回答您的问题。这两个文件可能有一个可能导致删除您的session@RomnickSusa是的,你是对的,我会编辑我的帖子。我只是想看看这是否是一个已知的问题。@RomnickSusa我希望这不太容易筛选,如果你有任何疑问,请告诉我。如果你将async:false
(或者无论现在同步调用的标准是什么)设置到ajax,你仍然会丢失会话数据吗?@Andrew不确定你的意思。如何/在何处将其设置为false?