Question: I have (maybe) an unusual issue with using `__FILE__` in a file within a file.

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?

EDIT1: Example

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:

`$bn = (int) filter_var(__FILE__, FILTER_SANITIZE_NUMBER_INT);`

…to get the article number, but realized it would get the 1 in the header instead.

EDIT2: Temporary Solution

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 `__FILE__` with $infile, like so:

```
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";

```

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.

Answer: 
While it's not as bad as I initially thought based on your description your code is still very fragile, and really only works by accident. If you have *any* other digits or hyphens it's going to go wrong, as below.

```
$infile = 'https://example-123.com/foo/42/bar/005-extremelynormalfile.html?x=8&y=9';

var_dump(
    filter_var($infile, FILTER_SANITIZE_NUMBER_INT),
    (int)filter_var($infile, FILTER_SANITIZE_NUMBER_INT)
);

```

Output:

```
string(12) "-12342005-89"
int(-12342005)

```

Sanitize functions are a blunt instrument for destroying data, and should only ever be used as a last resort when all other good sense has failed.

You need to use a proper parsing function to parse the url into its component parts, and then a simple regular expression to get what you want out of the filename.

```
function getIdFromURL($url) {
    $url_parts = parse_url($url);
    $path = $url_parts['path'];
    $path_parts = explode('/', $path);
    $filename = end($path_parts);
    if( preg_match('/^(\d+)/', $filename, $matches) ) {
        return (int)$matches[1];
    }
    return null;
}

var_dump(
    getIdFromURL($infile)
);

```

Lastly, a *lot* of people are tempted to cram as much logic as possible into a regular expression. If I wanted to the above could be a single regex, but it would also be rigid, unreadable, and unmaintainable. Use regular expressions sparingly, as there's nearly always a parser/library that already does what you want, or the majority of it.
Quickly threw together a function that gets the url from the page as a variable, and replaced all occurrences of `__FILE__` with that variable, and it worked correctly. Assuming the user cannot edit the URL / URI in any way, this should work well enough.