【发布时间】:2015-01-27 01:23:56
【问题描述】:
我一直在寻找将变量id 从 index.php 转移到(以及其他)update-check.php 的方法。最初,这导致了无限的加载时间——即页面在 localhost 上永远等待。我将其缩小为 session_start(); 导致此错误。如果我删除 session_start(); 并将 'stories/'. $_SESSION['id'] .'.txt'; 替换为实际的 id 变量(如 'stories/film.txt';),则加载任何内容都没有问题——除了我需要该变量。
我还注意到我有时会在控制台中收到Failed to load resource: net::ERR_CACHE_MISS。
我试图修补并找出为什么会发生这种情况以及如何解决它,但是我的大脑很疼,我希望在这种情况下有更多经验的人能够在这个问题上提供一些帮助(它是值得一提的是,我不习惯使用 PHP 或后端,来自前端作为设计师)。
编辑——所以问题不在于 PHP,而在于这个脚本 是否可以调整此脚本以在保持功能的同时发挥出色?
/**
* AJAX long-polling
*
* 1. sends a request to the server (without a timestamp parameter)
* 2. waits for an answer from server.php (which can take forever)
* 3. if server.php responds (whenever), put data_from_file into #response
* 4. and call the function again
*
* @param timestamp
*/
function getContent(timestamp)
{
var queryString = {'timestamp' : timestamp};
$.ajax(
{
type: 'GET',
url: 'update-check.php',
data: queryString,
success: function(data){
// put result data into "obj"
var obj = jQuery.parseJSON(data);
// put the data_from_file into #response
$('#response').html(obj.data_from_file);
// call the function again, this time with the timestamp we just got from server.php
getContent(obj.timestamp);
//timeout to avoid endless loading? didn't make a difference
timeout: 3000 // sets timeout to 3 seconds
}
}
);
}
// initialize jQuery
$(function() {
getContent();
});
更新检查.php
<?php
session_start();
/**
* Server-side file.
* This file is an infinitive loop. Seriously.
* It gets the file data.txt's last-changed timestamp, checks if this is larger than the timestamp of the
* AJAX-submitted timestamp (time of last ajax request), and if so, it sends back a JSON with the data from
* data.txt (and a timestamp). If not, it waits for one seconds and then start the next while step.
*
* Note: This returns a JSON, containing the content of data.txt and the timestamp of the last data.txt change.
* This timestamp is used by the client's JavaScript for the next request, so THIS server-side script here only
* serves new content after the last file change. Sounds weird, but try it out, you'll get into it really fast!
*/
// set php runtime to unlimited
set_time_limit(0);
/* $data_source_file = 'stories/film.txt'; */
$data_source_file = 'stories/'. $_SESSION['id'] .'.txt';
// main loop
while (true) {
// if ajax request has send a timestamp, then $last_ajax_call = timestamp, else $last_ajax_call = null
$last_ajax_call = isset($_GET['timestamp']) ? (int)$_GET['timestamp'] : null;
// PHP caches file data, like requesting the size of a file, by default. clearstatcache() clears that cache
clearstatcache();
// get timestamp of when file has been changed the last time
$last_change_in_data_file = filemtime($data_source_file);
// if no timestamp delivered via ajax or data.txt has been changed SINCE last ajax timestamp
if ($last_ajax_call == null || $last_change_in_data_file > $last_ajax_call) {
// get content of data.txt
$data = file_get_contents($data_source_file);
// put data.txt's content and timestamp of last data.txt change into array
$result = array(
'data_from_file' => $data,
'timestamp' => $last_change_in_data_file
);
// encode to JSON, render the result (for AJAX)
$json = json_encode($result);
echo $json;
// leave this loop step
break;
} else {
// wait for 1 sec (not very sexy as this blocks the PHP/Apache process, but that's how it goes)
sleep( 1 );
continue;
}
}
【问题讨论】:
-
如果这是在 Linux 上,可能是因为该进程没有创建会话文件的特权/权限,通常在
/tmp。 -
这个是用在ajax中的吗?请注意,当您打开一个会话时,它会被脚本阻止,因此当另一个脚本尝试访问同一个会话(例如同时使用 ajax 脚本)时,只有一个可以工作,其他脚本必须等到它完成。
-
@wallyk 我在我的 MacBook 上本地运行它,不知道这是否算作 Linux,但我检查了文件夹权限,它们看起来没问题。如果需要,如何检查会话文件权限?
-
@jeroen 是的,虽然我只有一个 ajax 脚本。有没有办法检测并发脚本?
-
while (true) { ... }总是在自找麻烦。
标签: php debugging variables session load-time