title: php __FILE__ inside includes?
body: <p>I have (maybe) an unusual issue with using <code>__FILE__</code> in a file within a file.</p>
  
  <p>I created a snippet of code (in the php 5 my server mandates) to take elements of the current filename and put it into a variable to use later. After some headache, I got it working totally fine. However, I realized I didn't want to have to write it every time and realized "oh no, if I include this it's only going to work on the literal filename of the include". If I wanted to grab the filename of the page the user is looking at, as opposed to the literal name of the included file, what's the best approach? Grab the URL from the address bar? Use a different magic variable?</p>
  
  <p>EDIT1: Example</p>
  
  <p>I probably should have provided an example in the first draft, pfft. Say I have numbered files, and the header where the include takes place in is 01header.php, but the file it's displayed in is Article0018.html. I used:</p>
  
  <p><code>$bn = (int) filter_var(__FILE__, FILTER_SANITIZE_NUMBER_INT);</code></p>
  
  <p>…to get the article number, but realized it would get the 1 in the header instead.</p>
  
  <p>EDIT2: Temporary Solution</p>
  
  <p>I've """solved""" the issue by creating a function to get the URL / URI and putting it into the variable $infile, and replaced all former appearances of <code>__FILE__</code> with $infile, like so:</p>
  
  <pre><code>function getAddress() {
  $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
  return $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];}
  $infile = urlencode(getAddress());
  $bn = (int) filter_var($infile, FILTER_SANITIZE_NUMBER_INT);
  echo "$bn";
  </code></pre>
  
  <p>So if the file the user is looking at is called "005-extremelynormalfile.html", I can display the number 5 inside the page, e.g., to say it's article number five.</p>