title: Strange & Insane PHP Bug, Page load after clearing browser history causes script to run multiple times
body: <p>Basically I am creating a php framework for a University project
  everything works except for a strange error making all PHP SESSION code duplicate after the request life cycle.</p>
  
  <p>I have edited my original question, hopefully its simpler and more to the point.
  I have done some further tests and now I can see what is happening but I cant understand why.</p>
  
  <p>I am using DOMDocument for templating and it is RUNNING UNEXPECTED CODE
  and causing the script to produce bizarre results by running any code related to the session after the script has ended.</p>
  
  <p>Basically I am testing the sessions and the request life cycle with a simple counter function.</p>
  
  <p>Here is the code and request life cycle:</p>
  
  <pre><code>//Session config from config file:
  const SESSION = [
  'name' =&gt; 'MAPSID',
  'storage' =&gt; 'files',
  'options' =&gt; [
      //'read_and_close' =&gt; true,
      'cookie_lifetime' =&gt; false,
      'use_strict_mode' =&gt; true,
      'use_only_cookies' =&gt; 1,
      'cookie_httponly' =&gt; 1,
      'use_trans_sid' =&gt; 0,
      //Ensure this is true for production:
      'cookie_secure' =&gt; 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-&gt;start(function ($request, $response){
      $response-&gt;capture(
          $request-&gt;route()
      );
      $response-&gt;send();
  });
  
  //App start method:
  public function start(callable $initfunc=null)
  {
      //Checks no illegal patterns in the URL, etc
      if ($this-&gt;request-&gt;is_valid()) {
          if (is_callable($initfunc)) {
              call_user_func_array(
                  $initfunc,[$this-&gt;request, $this-&gt;response]
              );
          }
      }else{
          $this-&gt;response-&gt;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\/\?\&amp;\=\-\%]+$/", $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-&gt;create_router(new Router(
           $this-&gt;path,
           $this-&gt;client-&gt;user)
      );
      include APP.'/routes/routes.php';
      return $route-&gt;response();
  }
  
  //Capture response:
  public function capture($res)
  {
      if ($res === false) {
          $this-&gt;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-&gt;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-&gt;response = $res;//there is no error
      }elseif (is_null($response)) {
          echo "Deal with this later";
      } else {
          $this-&gt;invalid();
      }
  }
  
  //Valid response method:
  public function valid($response)
  {
      $mapdom = file_get_contents(APP.'/main.html');
      $mapdom = new HTMLDom($mapdom);
      $root = $mapdom-&gt;getElementById('root');
      $response = new HTMLDom($res);
      $domelem = $response-&gt;body-&gt;firstChild;
      $import = $mapdom-&gt;importNode($domelem, true);
      $root-&gt;appendChild($import);
      $mapdom-&gt;save_html();
      $this-&gt;response = $mapdom-&gt;dom;
  }
  
  //send response:
  public function send(){
      foreach ($this-&gt;headers as $header) {
          header($header);
      }
      $this-&gt;response = 'some string';//No error
      echo $this-&gt;response;
  }
  </code></pre>
  
  <p>Basically wen I use $this->valid() function the increment triples for the first request, then doubles,
  If I dont use it it doubles for the first request and then increments by one,
  There is obviously a pattern here, I am struggling to figure it out.
  Tried using the debugger in PHPStorm, no error, its only the var_dump() output which is out of whack, makes me wonder, what is the error</p>
  
  <p>I hope the question is not ambiguous, any advice would be greatly appreciated! Happy to provide full source code
  Please can someone tell me where I am going wrong
  Thanks</p>