title: How to create a common function in codeigniter for checking if session exist?
body: <p>I am creating an application where in users have to login to access various modules. I need to check if the user session exist before providing access to each module.</p>
  
  <p>Now I am checking session like this in each function / module / controller to avoid unauthorized access.</p>
  
  <pre><code>if($this-&gt;session-&gt;userdata('userId')!=''){
     do something;  }
  </code></pre>
  
  <p>Is there a better way to do this? can I have common function similar like </p>
  
  <pre><code>sessionExist();
  </code></pre>
  
  <p>such that it can be called from any module / controller / function which is common to the whole project?</p>
  
  <p>If so where should I write this common function such that it can be called from anywhere.</p>