PHP: Hit Counter

Current Hit Count: 298


The code used for index.php is below:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="../styles.css">
    <title>PHP: Hit Counter</title>
</head>
<body>
    <h1 class="lesson-h1">PHP: Hit Counter</h1>
        
    <?php
        $file_name = 'counter.txt';

        function inc_count(){
            global $file_name;

            if(file_exists($file_name)){
                $current_value = file_get_contents($file_name);
            } else {
                $current_value = 0;
            }
            file_put_contents($file_name, ++$current_value);
        }

        inc_count();

        $counter = file_get_contents($file_name);
        echo "<h3>Current Hit Count: $counter</h3>";

        include('../show_code.php');
        show_code('index.php');
        show_code('counter.txt');
    ?>
</body>
</html>

The code used for counter.txt is below:


298