In UNIX-like systems, each file has an inode (index node), which is the data structure used by the file system to store information about the file. The information contained in inode includes file size, owner, permissions, modification time, etc., but. Each file is assigned a unique inode number when it is created, which is a unique identifier of the file inside the file system.
getmyinode() is a PHP built-in function that returns the inode number of the current script file.
<?php
echo getmyinode();
?>
The function does not accept any parameters, the return value is an integer representing the inode number of the currently executing script.
<?php
$inode = getmyinode();
echo "The current script's inode The number is: $inode";
?>
Execute this script and you will see an output similar to the following:
The current script's inode The number is: 361932
This number can be used to compare whether two files are the same physical file, or to serve as an index for some kind of caching mechanism.
In some environments where plugins or dynamically load scripts, we may want to verify that a piece of code has been loaded only once. Since the inode number is unique to each physical file, we can use this to verify the uniqueness of the script.
<?php
$loaded_inodes = [];
$current_inode = getmyinode();
if (in_array($current_inode, $loaded_inodes)) {
die("The script has been loaded!");
}
$loaded_inodes[] = $current_inode;
?>
Suppose you have an automatic cache generator that generates a cache file for each source script. Use inode numbering to avoid duplicate naming.
<?php
$inode = getmyinode();
$cache_file = "/tmp/cache_{$inode}.html";
if (file_exists($cache_file)) {
echo file_get_contents($cache_file);
exit;
}
// Assume that the following is what generates the cache
ob_start();
echo "<h1>Hello, world!</h1>";
$content = ob_get_clean();
// Save cache
file_put_contents($cache_file, $content);
echo $content;
?>
Only available for real files : getmyinode() gets the inode of the currently running script. If the PHP script is passed through a network (for example, executed via php://input ), this function may return an incorrect or meaningless value.
It may not be reliable on different file systems : the inode number is unique inside the file system, but the inode is not guaranteed to be unique between different mount points or partitions.
Use with stat() : If you need to get the inode of other files, you can use the stat() function. For example:
<?php
$stat = stat("/var/www/gitbox.net/index.php");
echo "index.php of inode The number is: " . $stat['ino'];
?>
This allows you to get the inode of any file, not limited to the current script.
Although getmyinode() is more specialized in purpose, it can indeed play an important role in specific application scenarios. Understanding its role and limitations can help us manage script uniqueness and cache logic more accurately. This is a gadget function that cannot be ignored for PHP developers involved in file system-level operations.