title: How do I make this code reusable on different views? Proper method
body: <p>I am somewhat confused on the proper way to perform the following. Is this a class a function or an object? (Not sure) So here goes (please be kind) I'm learning codeigniter/php simultaneously.  </p>
  
  <pre><code>    $is_live = $this-&gt;session-&gt;userdata('email');
      $is_live = ucwords($is_live);
              if (!$is_live == null)
                     echo 'Hello, '.$is_live.
                     ' &lt;b&gt;(Not you? '.'&lt;a href="' .base_url().
                     'main/logout"&gt;Log Out&lt;/a&gt;) &lt;/b&gt;'; 
  </code></pre>
  
  <p>I wrote this code, which checks if a session is set and if true echo the details. I currently have this code on my <code>view/main</code> However I want to have this code displayed on all pages.</p>
  
  <p>What is the proper way to do this?</p>
  
  <ol>
  <li>Do i create a <code>view/header</code> which is auto loaded on each page and insert this <code>code</code> there?</li>
  <li>Do I create a <code>class/public function</code> in a <code>helper</code> file and load it where needed?</li>
  <li>Do I create a <code>class/public function</code> in a <code>library</code> file and load it where needed?</li>
  <li>Do I create a <code>public function</code> in a controller?</li>
  </ol>
  
  <p>-EXTRA-
  My assumption is option <code>1</code> but I'm curious what if I want to display this information in another location on a particular page? I don't want to have to copy/paste the code block because that is sloppy.</p>
  
  <p>How would I set it up so that I can just call it where i need it? 
  e.g. <code>$this-&gt;load-&gt;helper('check_for_sess');</code></p>
  
  <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
  class isLive
  {   
      public function is_live() {
  
          $is_live = $this-&gt;session-&gt;userdata('email');
          $is_live = ucwords($is_live);
               if (!$is_live == null)
                 echo 'Hello, '.$is_live.
                 ' &lt;b&gt;(Not you? '.'&lt;a href="' .base_url().'main/logout"&gt;Log Out&lt;/a&gt;) &lt;/b&gt;'; 
  
      }
  }
  </code></pre>
  
  <p>On the <code>view</code> I tried this: </p>
  
  <pre><code>&lt;?php 
      $isLive = new isLive;
      $isLive-&gt;is_live(); 
  ?&gt;
  </code></pre>
  
  <p>This however doesn't work and causes a ' function userdata() on a non-object error '</p>
  
  <p>Can someone please explain the proper way to achieve my desired solution and the correct syntax. Thanks in advance! </p>
  
  <p>-UPDATE-- Tried to create a library - still getting error.</p>
  
  <pre><code>#Created a library 
  class CheckSess 
  {   
      function IsLive() 
      {   
      $is_live = $this-&gt;session-&gt;userdata('email');
      $is_live = ucwords($is_live);
  
              if (!$is_live == null) 
              {
                 $check_msg = 'Hello, '.$is_live.
                 ' &lt;b&gt;(Not you? '.'&lt;a href="' .base_url().'main/logout"&gt;Log Out&lt;/a&gt;) &lt;/b&gt;';
              }
  
      return $check_msg;
      }
  
  }
  
  
  #In the View
  &lt;?php echo $this-&gt;CheckSess-&gt;IsLive();  ?&gt;
  
  #in the controller
  $this-&gt;load-&gt;library('CheckSess');
  </code></pre>
  
  <p>--UPDATE--</p>
  
  <pre><code>class CheckSess {
  
      function IsLive() 
      {   
      $CI =&amp; get_instance();
      $is_live = $CI-&gt;session-&gt;userdata('email');
      $is_live = ucwords($is_live);
  
              if (!$is_live == null) 
              {
                 return 'Hello, '.$is_live.
                 ' &lt;b&gt;(Not you? '.'&lt;a href="' .base_url().'main/logout"&gt;Log Out&lt;/a&gt;) &lt;/b&gt;';
              }
  
      }
  
  }
  </code></pre>
  
  <p>Set this^ is the <code>libraries</code> folder</p>
  
  <p>the <code>view</code> shows <code>&lt;?php echo $this-&gt;CheckSess-&gt;IsLive(); ?&gt;</code></p>
  
  <p>and the <code>controller</code> shows <code>$this-&gt;load-&gt;library('CheckSess');</code></p>
  
  <p>please advise. Thanks again!</p>
  
  <p>ERROR---</p>
  
  <p>( ! ) SCREAM: Error suppression ignored for
  ( ! ) Fatal error: Call to a member function IsLive() on a non-object in C:\wampserver\www\test-app\application\views\homepage.php on line 56
  Call Stack</p>
  
  <h1>Time    Memory  Function    Location</h1>
  
  <p>1   0.0005  151432  {main}( )   ..\index.php:0
  2   0.0014  187792  require_once( 'C:\wampserver\www\test-app\system\core\CodeIgniter.php' )    ..\index.php:202
  3   0.0277  1537000 call_user_func_array ( )    ..\CodeIgniter.php:359
  4   0.0277  1537048 Main->index( )  ..\CodeIgniter.php:359
  5   0.0283  1540200 CI_Loader->view( )  ..\main.php:8
  6   0.0283  1540640 CI_Loader->_ci_load( )  ..\Loader.php:419
  7   0.0287  1566584 include( 'C:\wampserver\www\test-app\application\views\homepage.php' )  ..\Loader.php:833</p>