我正在调用firstpage.php它改变$ _SESSION ['Progress']会话值。但是当我调用progress.php获取$ _SESSION ['Progress']会话值时,它会保持“1”值。 progress.php不提供更新的值。我手动检查firstpage.php工作正常。但我不知道为什么progress.php没有给出更新的价值。
var progressInterval;
$.ajax({
type: "POST",
url: "firstpage.php",
cache: false,
data: {
first: first
},
success:function(result)
{
if(progressInterval) {
clearInterval(progressInterval); //if I remove this session still show "1" output
}
alert(result); // print session value. ex:- value- 1,2,3,4,5,6,7,8,Final=8
}
});
progressInterval = setInterval(function(){
$.ajax({
url:'progress.php',
type: 'get',
data: {"name":"Progress"},
dataType:'json',
success: function(data) {
$('.output').text((parseInt(data.progress))+"%"); // KEEP SHOWING 1% (why value does not change?????)
},
error:function(err){
console.log(err);
}
});
}, 1000);
firstpage.php
session_start();
$_SESSION['Progress'] = 0;
$data=0;
$progress=$_SESSION['Progress'];
foreach($album_data as $row) //assume foreach works fine.
{
$progress++;
$_SESSION['Progress'] = $progress;
$data.=$progress.",";
session_write_close();
}
}
echo $data." final=".$_SESSION['Progress']; //output-1,2,3,4,5,6,7,8 final=8
progress.php代码
session_start();
getProgress($_GET['name']);
function getProgress($filename) {
if (isset($_SESSION[$filename])) {
echo json_encode(array("progress" => $_SESSION[$filename]));
} else {
echo json_encode(array("progress" => 0));
}
}
答案 0 :(得分:2)
session_write_close();
结束当前会话并存储会话数据。
在循环的每次迭代中,您需要使用session_start();
答案 1 :(得分:2)
我在firstpage.php中更改了。它现在有效。
session_start();
$_SESSION['Progress'] = 0;
$data=0;
$progress=$_SESSION['Progress'];
foreach($album_data as $row) //assume foreach works fine.
{
session_start(); //we need to start session because we closed the session using session_write_close();
$progress++;
$_SESSION['Progress'] = $progress;
$data.=$progress.",";
session_write_close(); // need to close because we getting the value uses at progress.php.
}
}
session_start();
echo $data." final=".$_SESSION['Progress']; //output-1,2,3,4,5,6,7,8 final=8
答案 2 :(得分:0)
你试试这个。你有index.php,其中$_SESSION['Progress']
的获取值是index.php包含的
<强>的index.php 强>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
var progressInterval;
progressInterval = setInterval( function(){
$.ajax({
url: "progress.php",
data: { name: 1 }
}).done(function(msg) {
$("#test1").text(msg);
});
}, 2000);
</script>
</head>
<body>
<p id='test1' class='output'></p>
</body>
</html>
它将每2秒获取一次$_SESSION['Progress']
。
这里 progress.php 包含
session_start();
getProgress($_GET['name']);
function getProgress($filename) {
if (isset($_SESSION['Progress'])) {
echo json_encode(array("progress" => $_SESSION['Progress']));
} else {
echo json_encode(array("progress" => 0));
}
}
然后是 firstpage.php
session_start();
$_SESSION['Progress'] = 0;
/*
$data=0;
$progress=$_SESSION['Progress'];
foreach($album_data as $row) //assume foreach works fine.
{
$progress++;
$_SESSION['Progress'] = $progress;
$data.=$progress.",";
session_write_close();
}
}
echo $data." final=".$_SESSION['Progress']; //output-1,2,3,4,5,6,7,8 final=8
*/
firstpage.php仅适用于更改$_SESSION['Progress']
值。
尝试打开2个浏览器窗口。一个用于index.php,另一个用于firstpage.php。
保持index.php打开,同时将firstpage.php中的$_SESSION['Progress']
值更改为2(例如)并重新加载页面firstpage.php
。然后,您会发现$_SESSION['Progress']
的值index.php
在{"progress":2}
更改为{{1}}
所以我认为你在执行ajax请求时遇到了问题。