{"id":6677,"date":"2014-04-21T12:51:32","date_gmt":"2014-04-21T12:51:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/21\/creating-back-page-links-in-codeigniter-collection-of-common-programming-errors-2\/"},"modified":"2014-04-21T12:51:32","modified_gmt":"2014-04-21T12:51:32","slug":"creating-back-page-links-in-codeigniter-collection-of-common-programming-errors-2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/21\/creating-back-page-links-in-codeigniter-collection-of-common-programming-errors-2\/","title":{"rendered":"creating back page links in Codeigniter-Collection of common programming errors"},"content":{"rendered":"<p>I extend the session class by creating <code>\/application\/libaries\/MY_Session.php<\/code><\/p>\n<pre><code>class MY_Session extends CI_Session {\n\n    function __construct() {\n        parent::__construct();\n\n        $this-&gt;tracker();\n    }\n\n    function tracker() {\n        $this-&gt;CI-&gt;load-&gt;helper('url');\n\n        $tracker =&amp; $this-&gt;userdata('_tracker');\n\n        if( !IS_AJAX ) {\n            $tracker[] = array(\n                'uri'   =&gt;      $this-&gt;CI-&gt;uri-&gt;uri_string(),\n                'ruri'  =&gt;      $this-&gt;CI-&gt;uri-&gt;ruri_string(),\n                'timestamp' =&gt;  time()\n            );\n        }\n\n        $this-&gt;set_userdata( '_tracker', $tracker );\n    }\n\n\n    function last_page( $offset = 0, $key = 'uri' ) {   \n        if( !( $history = $this-&gt;userdata('_tracker') ) ) {\n            return $this-&gt;config-&gt;item('base_url');\n        }\n\n        $history = array_reverse($history); \n\n        if( isset( $history[$offset][$key] ) ) {\n            return $history[$offset][$key];\n        } else {\n            return $this-&gt;config-&gt;item('base_url');\n        }\n    }\n}\n<\/code><\/pre>\n<p>And then to retrieve the URL of the last page visited you call<\/p>\n<pre><code>$this-&gt;session-&gt;last_page();\n<\/code><\/pre>\n<p>And you can increase the offset and type of information returned etc too<\/p>\n<pre><code>$this-&gt;session-&gt;last_page(1); \/\/ page before last\n$this-&gt;session-&gt;last_page(2); \/\/ 3 pages ago\n<\/code><\/pre>\n<p>The function doesn&#8217;t add pages called using Ajax to the tracker but you can easily remove the <code>if( !IS_AJAX )<\/code> bit to make it do so.<\/p>\n<p>Edit: If you run to the error Undefined constant IS_AJAX, assumed IS_AJAX add the line below to <code>\/application\/config\/constants.php<\/code><\/p>\n<pre><code>define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) &amp;&amp; strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I extend the session class by creating \/application\/libaries\/MY_Session.php class MY_Session extends CI_Session { function __construct() { parent::__construct(); $this-&gt;tracker(); } function tracker() { $this-&gt;CI-&gt;load-&gt;helper(&#8216;url&#8217;); $tracker =&amp; $this-&gt;userdata(&#8216;_tracker&#8217;); if( !IS_AJAX ) { $tracker[] = array( &#8216;uri&#8217; =&gt; $this-&gt;CI-&gt;uri-&gt;uri_string(), &#8216;ruri&#8217; =&gt; $this-&gt;CI-&gt;uri-&gt;ruri_string(), &#8216;timestamp&#8217; =&gt; time() ); } $this-&gt;set_userdata( &#8216;_tracker&#8217;, $tracker ); } function last_page( $offset = 0, $key [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6677","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6677"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6677\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}