基本上,我正在为大学项目创建一个php框架。一切正常,除了一个奇怪的错误,使所有PHP SESSION代码在请求生命周期后重复。
我已经编辑了原始问题,希望它更简单,更具体。我已经做了一些进一步的测试,现在我可以看到发生了什么,但我不明白为什么。
我正在使用DOMDocument进行模板处理,并且正在运行意外的代码并在脚本结束后运行与会话相关的任何代码,从而导致脚本产生奇怪的结果。
基本上,我正在使用简单的计数器功能来测试会话和请求生命周期。
这里是代码,并请求生命周期:
//Session config from config file:
const SESSION = [
'name' => 'MAPSID',
'storage' => 'files',
'options' => [
//'read_and_close' => true,
'cookie_lifetime' => false,
'use_strict_mode' => true,
'use_only_cookies' => 1,
'cookie_httponly' => 1,
'use_trans_sid' => 0,
//Ensure this is true for production:
'cookie_secure' => false
],
];
session_name(SESSION['name']);
//index.php:
define('MAP_INITIALIZE', microtime(true));
require_once __DIR__.'/../env.php';
session_start(SESSION['options']);
require_once __DIR__.'/../vendor/autoload.php';
//Set Session counter to 0 if not set
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
}
var_dump("SESSION count before increment:");
//Shows perfectly first time, then increments by 3, then by 2,
//Instead of one
var_dump($_SESSION['count']);
//Increment code which duplicates god knows where
$_SESSION['count']++;
var_dump($_SESSION['count']);
$app = map\factory\MapFactory::create_app();
$app->start(function ($request, $response){
$response->capture(
$request->route()
);
$response->send();
});
//App start method:
public function start(callable $initfunc=null)
{
//Checks no illegal patterns in the URL, etc
if ($this->request->is_valid()) {
if (is_callable($initfunc)) {
call_user_func_array(
$initfunc,[$this->request, $this->response]
);
}
}else{
$this->response->invalid();
}
}
//request is_valid:
public function is_valid(){
if (filter_var($url, FILTER_SANITIZE_URL)) {
if ($url == '/') {
return true;
}
if (!preg_match_all("/^\/[A-Za-z0-9\/\?\&\=\-\%]+$/", $url)) {
return false;
}
if (preg_match_all("/\/\//", $url)) {
return false;
}
return true;
}else {
return false;
}
}
//Route Request:
//I dont think this should matter as its the default index page,
no routes used
public function route()
{
$route = $this->create_router(new Router(
$this->path,
$this->client->user)
);
include APP.'/routes/routes.php';
return $route->response();
}
//Capture response:
public function capture($res)
{
if ($res === false) {
$this->not_found();
}elseif (is_string($res)) {
//If I call this function, and then echo the response,
//$_SESSION['count'] double increments,
//and triple for the first request,
$this->valid($response);
//If I directly add the response without the valid method,
//then the first refresh increments by 2 and then by 1
//like so:
$this->response = $res;//there is no error
}elseif (is_null($response)) {
echo "Deal with this later";
} else {
$this->invalid();
}
}
//Valid response method:
public function valid($response)
{
$mapdom = file_get_contents(APP.'/main.html');
$mapdom = new HTMLDom($mapdom);
$root = $mapdom->getElementById('root');
$response = new HTMLDom($res);
$domelem = $response->body->firstChild;
$import = $mapdom->importNode($domelem, true);
$root->appendChild($import);
$mapdom->save_html();
$this->response = $mapdom->dom;
}
//send response:
public function send(){
foreach ($this->headers as $header) {
header($header);
}
$this->response = 'some string';//No error
echo $this->response;
}
基本上,我使用$ this-> valid()函数将第一个请求的增量增加三倍,然后增加一倍,如果我不使用它,它会为第一个请求加倍,然后再加一,显然这里有一个模式,我正在努力弄清楚。使用PHPStorm中的调试器进行了尝试,没有错误,仅是var_dump()输出超出了预期,这让我怀疑,这是什么错误
我希望这个问题不是模棱两可的,任何建议将不胜感激!乐意提供完整的源代码请有人告诉我我要去哪里错了谢谢
在回答自己的问题之前,我会说我很傻,但是希望这也会对其他人有所帮助。
使用任何前控制器模式或服务器重定向时,我必须确保在浏览器要求时将favicon.ico保留在公用文件夹的根目录中。并且由于Apache将所有不是有效文件的内容都重定向到index.php,因此,如果不是实际映像,则保留favicon.ico虚拟文件至关重要。
如果缺少此文件,apache将根据重写规则将其重定向到index.php,这将导致脚本再次运行,这可能会破坏会话变量并导致意外的问题和结果,这是因为我永远无法实现的梦想如果不是此答案,则猜测可能与该问题有关:https://stackoverflow.com/a/19761612/11995521