Page 1 of 1

PHP class working incorrectly

Posted: Wed Dec 02, 2009 4:01 pm
by st89
I am in the process of creating a PHP class that creates a HTML page but only displays content if the user has a certain web browser, version and has both JavaScript and cookies enabled.

This is my index.php:

Code: Select all

<?php
require("classes.php");
$page = new page;
$page->display_page();
?>
And this is the file it requires (classes.php):

Code: Select all

<?php
class page {
  public $getBrowser;
  public $browser;
  public $version;
  public $javascript;
  public $cookies;
  public $browserCompatible = false;
  public $title = "My Page";
  public $style = "stylesheet.css";
  public $header = "<h1>Header</h1>";
  public $content = "<p>Some content</p>";
  public $footer = "<p>Footer</p>";
 
  public function display_page() {
    echo "<html>\n<head>\n";
    $this->display_title();
    $this->display_styles();
    echo "</head>\n<body>\n";
    $this->display_header();
    $this->check_compatibility();
    $this->display_content();
    $this->display_footer();
    echo "</body>\n</html>\n";
  }
  public function check_compatibility() {
    $this->getBrowser = get_browser(null, true);
    $this->browser = $this->getBrowser['browser'];
    $this->version = $this->getBrowser['version'];
    $this->javascript = $this->getBrowser['javascript'];
    $this->cookies = $this->getBrowser['cookies'];
    
    switch($this->browser) {
      case "Firefox":
        if ($this->version < '1.0') {
          $this->content = "<p>You need at least Firefox 2.0 to use this site!</p>";
        } else if ($this->javascript != true) {
            $this->content = "<p>You need JavaScript enabled to use this site!</p>";  
          } else if ($this->cookies != true) {
              $this->content = "<p>You need cookies enabled to use this site!</p>";
            } else {
                $this->browserCompatibile = true;
              }  
        break;
      default:
        $this->content = "<p>You need Internet Explorer 7+, Firefox 1+, Safari 1.2+, Chrome 0.2+ or Opera 7.6+ to use this site!</p>";
    }
  }
  public function display_title() {
    echo "<title>".$this->title."</title>\n";
  }
  public function display_styles() {
    echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"".$this->style."\"></link>\n";
  }
  public function display_header() {
    echo "<div id=\"header\">".$this->header."</div>\n";
  }
  public function display_content() {
    echo "<div id=\"content\">".$this->content."</div>\n";
  }
  public function display_footer() {
    echo "<div id=\"footer\">".$this->footer."</div>\n";  
  }
}
?>
I am using Firefox 3.5 and switched JavaScript off, restarted Firefox and then loaded index.php. To my dismay, $content remained unchanged (when it should have changed on Line 38).

I would be grateful if someone could point out what I've done wrong! Thanks in advance for any help/tips/pointers :D

Re: PHP class working incorrectly

Posted: Wed Dec 02, 2009 4:50 pm
by Weiry
From what i know, get_browser only returns what the browser is capable of, it doesn't return whether the browser is allowed to run JavaScript etc or not though. So if it was in fact turned off, the get_browser for JavaScript will always return true, as the browser can in fact run JavaScript through it may be disabled.

I did a quick search and found a small example of how to check if JS is enabled. Not sure how useful it will be though.

Code: Select all

<html>
<head>
<script language="JavaScript">
window.location =
'http://www.yoursite.com/index1.php?js=1&token=<?php
echo time();
?>';
</script>
</head>
<body>
<p>Sorry, your browser does not support JavaScript...</p>
</body>
</html>
In index1.php, you check if $_GET['js'] is indeed 1 and if
$_GET['token'] is reasonably close to server's current time.