0

I am trying to cache each data that is fetched from the SQL Database. I have successfully made a cache mechanism but it cache only 1 name from the SQL Database.

The Process:

It only caches one player at the same time. Is it possible to create multiple files for each converted uuid?

<?php

                        $connection2 = mysqli_connect("localhost", "root", "admin", "intense");

                        require_once "includes/dbConfig.php";

                        $uuid = $_GET['uuid'];

                        $query2 = "SELECT * FROM irduels_stats WHERE uuid = '$uuid'";
                        $result2 = mysqli_query($connection2, $query2);

                        while($row = mysqli_fetch_array($result2)) {
                            $rows[] = $row;

                            $uuid = $row['uuid'];

                            echo "<img src='https://crafatar.com/avatars/" . $row['uuid'] . "?size=100' alt=\"...\" class=\"img-thumbnail\">";

                            $uuidName = $row['uuid'];
                            $uuidName = str_replace('-', '', $uuidName);

                            $cache_file = "players/data.json";

                            if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 60 * 5 ))) {
                                // Cache file is less than five minutes old.
                                // Don't bother refreshing, just use the file as-is.
                                $file = file_get_contents($cache_file);
                            } else {
                                // Our cache is out-of-date, so load the data from our remote server,
                                // and also save it over our cache for next time.
                                $file = file_get_contents("https://sessionserver.mojang.com/session/minecraft/profile/". $uuidName ."");
                                file_put_contents($cache_file, $file, LOCK_EX);
                            }

                            $json_response = file_get_contents("data.json");
                            $data = json_decode($json_response);
                            $uuidConverted = $data->name;

                            echo "<blockquote class=\"blockquote playerName\">";

                            echo "<h2 class=\"mb-0\">". $uuidConverted ." <span class=\"badge badge-secondary\">Rank here</span></h2>";
                            echo "<footer class=\"blockquote-footer\">Viewing stats of <cite title=\"Source Title\">". $uuidConverted ."</cite></footer>";
                        }

                        echo "</blockquote>";

                        mysqli_close($connection2); //Make sure to close out the database connection

                        ?>
Clock
  • 17
  • 6
  • Its abit broke, change `$cache_file = "players/data.json";` to `$cache_file = "players/data.{$uuidName}.json";` and fix how it loads the json `file_get_contents("data.json");` to `file_get_contents($cache_file);` – Lawrence Cherone Dec 23 '17 at 03:22
  • You might also want to fix the sql injection problem. [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Lawrence Cherone Dec 23 '17 at 03:24
  • And if your only using `uuid` from the select there is no need to use `*` in your query. – Lawrence Cherone Dec 23 '17 at 03:27
  • Thanks for the help, do you know how to automatically create the cache files? I had to manually create files for each – Clock Dec 23 '17 at 04:01
  • file_put_contents will create fine if you got the proper permissions on the server. cache folder needs to be writable, and owned by the webserver user, prob www-data – Lawrence Cherone Dec 23 '17 at 04:07
  • Thank you so much for you help – Clock Dec 23 '17 at 09:37

0 Answers0