title: What's the best way to open last page opened even after browser is closed?
body: <p>I have this code which will redirect you to the last page you opened if you go to <code>subdirectoryname/index.php</code>. It's working but I find it really silly. Does anyone have a better idea on how i can achieve this more elegantly?</p>
  
  <p>Do not confuse this with window.history.back();
   I am trying to make the browser remember your last page opened. So even if you close your browser. and open the website tomorrow again. www.example.com You'll be redirected to www.example.com/lastpageyouopened.php</p>
  
  <p><strong>setCookieLastPage.php</strong></p>
  
  <pre><code>&lt;?php
  setcookie(
    'LastPageVisited',
    $_POST['lastpagevisited'],
    time() + 199999999
  );
  ?&gt;
  </code></pre>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>&lt;?php include_once 'header.php'; ?&gt;
  
  &lt;a href="&lt;?php if(isset($_COOKIE['LastPageVisited'])){echo $_COOKIE['LastPageVisited'];}else{ echo 'defaultpage.php';} ?&gt;"  class="hiddenlink-lastpageopened"&gt;&lt;/a&gt;
  
  
  &lt;?php include_once 'footer.php'; ?&gt;
  </code></pre>
  
  <p><strong>custom.js</strong></p>
  
  <pre><code>if($(location).attr('pathname')  == "/subdirectoryname/" || $(location).attr('pathname') == "/subdirectoryname/index.php"  || $(location).attr('pathname') == "/subdirectoryname/index.php/" )
  {
      var lastvisitedpagelink = $('.hiddenlink-lastpageopened').attr('href');
      setTimeout(function(){
      window.location.href = lastvisitedpagelink;
      },300);
  }
  runSETCookieLastPageVisited(window.location.href);
  
  
  function runSETCookieLastPageVisited(lastpagevisited)
  {
  
      if($(location).attr('pathname')  != "/subdirectoryname/" || $(location).attr('subdirectoryname') != "/play2win/index.php"  || $(location).attr('subdirectoryname') != "/play2win/index.php/" )
      {
          $.ajax(
              {
                 type: "POST",
                 url: "setCookieLastPage.php",
                 data: {"lastpagevisited":lastpagevisited},
                 datatype: "json",
                 cache: false,
                 success: function(data)
                 {
                   //alert(data);
                 }
              });
      }
  }
  </code></pre>
  
  <p><strong>NOTE:</strong> I am using a subdirectory of a different website project for now because this project still doesn't have its own domain. When we move it to it's own domain, there will no longer be a <strong>/subdirectoryname/</strong> and the path to index.php will just be "index.php" and not "subdirectoryname/index.php"</p>