Advertisement
Temporelucis

statRequest.php

Jun 27th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.81 KB | None | 0 0
  1. <?php
  2.   session_id("statistics");                         //Selects this ID
  3.   session_start();                                  //Starts a session
  4.  
  5.   $tier = $_REQUEST['filename'];
  6.   $var_name = $_REQUEST['id'];
  7.  
  8.   if (isset($_SESSION["CREATION_DATE"])) {          //Sets the creation date
  9.     if ($_SESSION["CREATION_DATE"] != date("Y-m-d")) {
  10.       session_destroy();                            //If it is a different day it destroys the information
  11.       $_SESSION["CREATION_DATE"] = date("Y-m-d");   //the session variable hold
  12.     } else {
  13.       $_SESSION["CREATION_DATE"] = date("Y-m-d");
  14.     }
  15.   }
  16.  
  17.   if (!isset($_SESSION[$tier]))
  18.     $_SESSION[$tier] = getData($tier);
  19.  
  20.   function getData ($new_tier) {                    //This function downloads the statistics from the official smogon website
  21.         $filename = "http://www.smogon.com/stats/date/moveset/".$new_tier;
  22.         $contents = null;
  23.         $i = 0;
  24.         while ($contents == null) {
  25.             $year = date("Y");
  26.             $month = date("m")-$i;
  27.             if ($month < 10)
  28.                 $month = "0".$month;
  29.             $date = $year."-".$month;
  30.             if (intval(get_http_response_code(str_replace("date", $date, $filename))) < 400) {
  31.                 $contents = file_get_contents(str_replace("date", $date, $filename));
  32.             } else {
  33.                 $contents = null;
  34.             }
  35.             $i++;
  36.             if ($i > 10)
  37.                 $contents = "not found!";
  38.         }
  39.         $contents = str_replace("\n", '" + "', $contents); //Replaces \n with ' + ' to concat each line of the text
  40.         $contents = preg_replace('/\s+/', ' ', $contents); //Removes white spaces
  41.         $contents = strtolower($contents);                 //Makes everything lower case
  42.         return $contents;
  43.     }
  44.     function get_http_response_code($theURL) {
  45.         $headers = get_headers($theURL);
  46.         return substr($headers[0], 9, 3);
  47.     }
  48.  
  49.   echo "<script>parent.".$var_name.' = "'.$_SESSION[$tier].'"; </script>';
  50. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement