title: passing parameters to php include/require construct
body: <p>I've read quite a few posts that are very similar to the question I'm about to ask, but I just wanted to be sure that there wasn't a more sophisticated way to do this.  Any feedback is greatly appreciated.</p>
  
  <p>I want to create a mechanism to check whether or not <strong>a logged-in user has access to the php script that is currently being called</strong>.  If so, the script will continue on; if not, the script just fails out using something like <code>die('you have no access')</code>.  </p>
  
  <p>I came up with two ways of accomplishing this:</p>
  
  <p>(please assume my session stuff is coded/working fine - i.e. I call session_start(), set up the session vars properly and etc)</p>
  
  <ol>
  <li><p>Define a global variable first, then check the global variable in a required header file. For example:</p>
  
  <p>Content of current_executing_script.php:    </p>
  
  <pre><code>// the role the logged in user must have to continue on   
  $roleNeedToAccessThisFile = 'r';
  require 'checkRole.php''
  </code></pre>
  
  <p>Content of checkRole.php:  </p>
  
  <pre><code>if ($_SESSION['user_role'] != $roleNeedToAccessThisFile) die('no access for you');
  </code></pre></li>
  <li><p>Define a function within the header file and call the function immediately after including/requiring it:</p>
  
  <p>Content of checkRole.php:</p>
  
  <p><pre><code>function checkRole($roleTheUserNeedsToAccessTheFile) {
      return ($_SESSION['user_role'] == $roleTheUserNeedsToAccessTheFile);
  }</code></pre></p>
  
  <p>Content of current_executing_script.php:  </p>
  
  <pre><code>require 'checkRole.php';
  checkRole('r') or die('no access for you');</code></pre></li>
  </ol>
  
  <p>I'm wondering if there is a way to basically just pass a parameter to checkRole.php as part of the include or require construct?</p>
  
  <p>Thanks in advance.</p>