Advertisement
Guest User

Untitled

a guest
Jun 10th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.92 KB | None | 0 0
  1. <?php
  2.     error_reporting(2048);
  3.     class Auth {
  4.         public $logged_in = false;
  5.         private $static_username = 'user';
  6.         private $static_password = 'password';
  7.        
  8.         public $username = '';
  9.        
  10.         public function login($username, $password) {
  11.             if (($username == $this->static_username) and ($password == $this->static_password)) {
  12.                 $this->username = $username;
  13.                 $this->logged_in = true;
  14.             }
  15.             return $this->logged_in;
  16.         }
  17.     }
  18.    
  19.     class Cache {
  20.         private $default_cache_time = 30;
  21.         private $cache_folder = 'cache';
  22.        
  23.         public function store($content, $key = 'default', $time = null) {
  24.             $cache_file = $this->default_cache_folder.'/'.$key;
  25.             if ($time !== null) {
  26.                 $cache_life = $time;
  27.             } else {
  28.                 $cache_life = $this->default_cache_time;
  29.             }
  30.            
  31.             if ( ! file_exists($cache_file)) {
  32.                 $file = fopen($cache_file, 'w+');
  33.                 fwrite($file, $content);
  34.                 fclose($file);
  35.                
  36.                 return $content;
  37.             }
  38.            
  39.             $cache_modified_time = filemtime($cache_file);
  40.            
  41.             if ((time() - $cache_modified_time) >= $cache_life) {
  42.                 file_put_contents($cache_file, $content);
  43.             } else {
  44.                 $content = file_get_contents($cache_file);
  45.             }
  46.             return $content;
  47.         }
  48.     }
  49.    
  50.     $auth = new Auth;
  51.     $cache = new Cache;
  52.    
  53.     if ($auth->logged_in == true) {
  54.         $header_content = 'Website Name. The date is: '.date('m/d/Y');
  55.         $header = $cache->store($header_content, 'header', 30);
  56.        
  57.         $body_content = 'Welcome '.$auth->username.'! Current time is: '.date('h:i:s A');
  58.         $body = $cache->store($body_content, 'body', 45);
  59.        
  60.         $footer_content = 'Current time is: '.date('h:i:s A').'. Random number is: '.mt_rand(1,100).'.';
  61.         $footer = $cache->store($footer_content, 'footer', 60);
  62.        
  63.         echo $header.'<br/>';
  64.         echo $body.'<br/>';
  65.         echo $footer.'<br/>';
  66.     } else {
  67.         $static_content = 'This is static guest content that will be cached.';
  68.         ob_start(array($cache, 'store'));
  69.         echo $static_content;
  70.         ob_end_flush();
  71.     }
  72.    
  73. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement