Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(2048);
- class Auth {
- public $logged_in = false;
- private $static_username = 'user';
- private $static_password = 'password';
- public $username = '';
- public function login($username, $password) {
- if (($username == $this->static_username) and ($password == $this->static_password)) {
- $this->username = $username;
- $this->logged_in = true;
- }
- return $this->logged_in;
- }
- }
- class Cache {
- private $default_cache_time = 30;
- private $cache_folder = 'cache';
- public function store($content, $key = 'default', $time = null) {
- $cache_file = $this->default_cache_folder.'/'.$key;
- if ($time !== null) {
- $cache_life = $time;
- } else {
- $cache_life = $this->default_cache_time;
- }
- if ( ! file_exists($cache_file)) {
- $file = fopen($cache_file, 'w+');
- fwrite($file, $content);
- fclose($file);
- return $content;
- }
- $cache_modified_time = filemtime($cache_file);
- if ((time() - $cache_modified_time) >= $cache_life) {
- file_put_contents($cache_file, $content);
- } else {
- $content = file_get_contents($cache_file);
- }
- return $content;
- }
- }
- $auth = new Auth;
- $cache = new Cache;
- if ($auth->logged_in == true) {
- $header_content = 'Website Name. The date is: '.date('m/d/Y');
- $header = $cache->store($header_content, 'header', 30);
- $body_content = 'Welcome '.$auth->username.'! Current time is: '.date('h:i:s A');
- $body = $cache->store($body_content, 'body', 45);
- $footer_content = 'Current time is: '.date('h:i:s A').'. Random number is: '.mt_rand(1,100).'.';
- $footer = $cache->store($footer_content, 'footer', 60);
- echo $header.'<br/>';
- echo $body.'<br/>';
- echo $footer.'<br/>';
- } else {
- $static_content = 'This is static guest content that will be cached.';
- ob_start(array($cache, 'store'));
- echo $static_content;
- ob_end_flush();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement