Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
奇怪的&;疯狂的PHP错误,清除浏览器历史记录后的页面加载会导致脚本多次运行_Php_Debugging_Session_Domdocument_Libxml2 - Fatal编程技术网

奇怪的&;疯狂的PHP错误,清除浏览器历史记录后的页面加载会导致脚本多次运行

奇怪的&;疯狂的PHP错误,清除浏览器历史记录后的页面加载会导致脚本多次运行,php,debugging,session,domdocument,libxml2,Php,Debugging,Session,Domdocument,Libxml2,基本上,我正在为一个大学项目创建一个php框架 除了一个奇怪的错误使所有PHP会话代码在请求生命周期后重复之外,其他一切都正常工作 我已经编辑了我的原始问题,希望它更简单、更切题。 我已经做了一些进一步的测试,现在我可以看到发生了什么,但我不明白为什么 我正在使用DOMDocument进行模板制作,它正在运行意外的代码 以及在脚本结束后运行与会话相关的任何代码,从而使脚本产生奇怪的结果 基本上,我用一个简单的计数器函数测试会话和请求生命周期 以下是代码和请求生命周期: //Session con

基本上,我正在为一个大学项目创建一个php框架 除了一个奇怪的错误使所有PHP会话代码在请求生命周期后重复之外,其他一切都正常工作

我已经编辑了我的原始问题,希望它更简单、更切题。 我已经做了一些进一步的测试,现在我可以看到发生了什么,但我不明白为什么

我正在使用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()函数,第一个请求的增量是三倍,然后是两倍, 如果我不使用它,第一个请求会加倍,然后增加1, 很明显这里有一个模式,我正在努力找出它。 尝试在PHPStorm中使用调试器,没有错误,只是var_dump()输出不正常,让我想知道,错误是什么

我希望问题不要含糊不清,任何建议都将不胜感激!很高兴提供完整的源代码 有人能告诉我哪里出了问题吗
谢谢

在我回答自己的问题之前,我会说我很傻,但希望这也能帮助别人

当使用任何前端控制器模式或服务器重定向时,我必须确保在浏览器请求时在公用文件夹的根目录中保留favicon.ico。 由于Apache将所有无效文件重定向到index.php,因此如果不是实际图像,则保留favicon.ico伪文件至关重要


如果缺少此文件,apache将根据重写规则将其重定向到index.php,这将导致脚本再次运行,这可能会损坏会话变量,并产生意外问题和结果,因为我做梦也想不到会有问题与此相关,如果不是因为这个答案:

在我回答自己的问题之前,我会说我很傻,但希望这也能帮助其他人

当使用任何前端控制器模式或服务器重定向时,我必须确保在浏览器请求时在公用文件夹的根目录中保留favicon.ico。 由于Apache将所有无效文件重定向到index.php,因此如果不是实际图像,则保留favicon.ico伪文件至关重要


如果缺少此文件,apache将根据重写规则将其重定向到index.php,这将导致脚本再次运行,这可能会损坏会话变量,并产生意外问题和结果,因为我做梦也想不到会有问题与此相关,如果不是这个答案:

您是否将会话设置为自动启动?或者您正在调用
session_start()其他地方?您是否检查了HTTP头并确认收到GET和/或POST(大写)?或者
$\u服务器['request\u method']
正在发送正确的值?
Auth::form()
是否在其他任何地方调用?你试过让这个方法非静态吗?还有,
$app->response->send()做什么?我要为会话处理程序创建包装类,看看它是否修复了它。这个函数只是回显响应,我已经在单独的文件中添加了它的代码。如果它有帮助,我将添加生命周期的所有代码。会话是否设置为自动启动?或者您正在调用
session_start()其他地方?您是否检查了HTTP头并确认收到GET和/或POST(大写)?或者
$\u服务器['request\u method']
正在发送正确的值?
Auth::form()
是否在其他任何地方调用?你试过让这个方法非静态吗?还有,
$app->response->send()
do?我将为会话处理程序创建包装器类,看看它是否修复了该类。该函数只是回显响应,我已经在单独的文件中添加了该类的代码。如果有帮助,我将添加生命周期的所有代码