Jump to content

Using $this when not in object context in class.page.php on line 123


maexus

Recommended Posts

page::auth() checks $lockLevel against the userlevel. I'm using $this->lockLevel and I get this message? Why? It doesn't make sense to me. Getting any form of help on these boards is increasly difficult. I know I'm not asking difficult questions.

[code]
<?php
class page {
    var $lockLevel = 0;    
    function __construct($pageTitle){
        self::auth();
        $db = new db;
        echo
        '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'."\n".
        '<html>'."\n".
        '<head>'."\n".
        "<title>".BLOG_TITLE." - $pageTitle</title>"."\n".
        '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">'."\n".
        '<link rel="stylesheet" type="text/css" href="'.CSS_LOCATION.'">'."\n".
        '<body>'."\n".
        '<div id="shadow">'."\n".
        '<div id="wrap">'."\n".
        '<h1 title="face">face</h1>'."\n".
        '<ul id="mainmenu">'."\n".
        '<li><a href="'.BASE_URL.'/index.php" title="Home">Home</a></li>'."\n".
        '<li><a href="'.BASE_URL.'/portfolio.php" title="Portfolio">Portfolio</a></li>'."\n".
        '<li><a href="'.BASE_URL.'/projects.php" title="Projects">Projects</a></li>'."\n".
        '<li><a href="'.BASE_URL.'/about.php" title="About Me">About Me</a></li>'."\n".
        '<li><a href="'.BASE_URL.'/login.php" title="Login">Login</a></li>'."\n".
        '</ul>'."\n".
        '<div id="content">'."\n";
    }
    
    function __destruct() {
        echo
        "\n".
        '</div>'."\n".
        '</div>'."\n".
        '</div>'."\n".
        '<p id="copy">&copy; 2006 Maex Media</p>'."\n".
        '</body>'."\n".
        '</html>'."\n";
    }
    
    static function entries($multi = true){
        //---PAGINATION---//
        if($multi){
            $page_num = isset($_GET['page']) ? mysql_real_escape_string($_GET['page']) : 1;
            $from = $page_num * ENTRY_LIMIT - ENTRY_LIMIT;
            $query ="SELECT * FROM `".ENTRY_TABLE."` ORDER BY `id` DESC LIMIT $from,".ENTRY_LIMIT;
        }else{
            $query ="SELECT * FROM `".ENTRY_TABLE."` WHERE id='".mysql_real_escape_string($_GET['id'])."'";
        }
    
        //Get Entries
        $results = mysql_query($query) or die(mysql_error());
        while ($entry = mysql_fetch_assoc($results)){
            //Format Entry Body
            $entry['body'] = nl2p(strings::bbcode($entry['body']));
            //Display The Entry
            echo
            '<div class="entry">'."\n".
            '<h2>'.$entry['subject'].'</h2>'."\n".
            '<p class="timestamp">'.strings::convert_timestamp($entry['timestamp']).'</p>'."\n".
            $entry['body']."\n".
            '</div>'."\n\n";
        }
        
        //---PAGINATION---//
        if($multi == 'yes'){
            $totalRows = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM ".ENTRY_TABLE))or die(mysql_error());
            $totalPages = $totalRows[0] / ENTRY_LIMIT;
            $totalPages = ceil($totalPages);
        
            if (!isset($countdown)){
                $countdown = $totalRows[0];
            }
            if (!isset($page)){
                $page = $page_num;
            }
            if (!isset($page1)){
                $page1 = 1;
            }
            if ($totalPages > 1){
                echo '<p class="pagination">Page: ';
                while ($countdown >= 0){
                    if (($page1 >= 1) && ($page1 <= $totalPages)){        
                        echo "<a href=\"?page=$page1\">$page1</a> ";
                        $page1 = $page1 + 1;
                    }
                    $countdown = $countdown - ENTRY_LIMIT;
                    $page = $page + 1;
                }
                echo '</p>';
            }
        }
    }
    
    static function auth(){
        if(isset($_COOKIE['token'])){
            if(strlen($_COOKIE['token']) == TOKEN_LENGTH /*&& preg_match("#[a-zA-Z0-9]#", $_COOKIE['token'])*/){
                $query = "SELECT * FROM `".USER_TABLE."` WHERE `token` = '".mysql_real_escape_string($_COOKIE['token'])."'";
                $results = mysql_query($query) or die(mysql_error());
                $row = mysql_fetch_assoc($results);
                if(mysql_num_rows($results) == 1){
                    /* FIELDS AVAILABLE IN ORDER
                    id
                    username
                    password
                    lost_pw_hint
                    userlevel
                    reg_timestamp
                    reg_ip
                    last_ip
                    email
                    token                
                    */
                    $USER = array('name'=>$row['username'], 'level'=>$row['userlevel']);
                }else{
                    $USER = array('name'=>'Guest', 'level'=>1);
                }
            }else{//If user has altered the token cookie to be more or less than predefined token length
                $USER = array('name'=>'Hacker', 'level'=>0);
            }
        }else{
            $USER = array('name'=>'Guest', 'level'=>1);
        }
        
        if($USER['level'] < $this->lockLevel){
            header("Location".BASE_URL."/login.php");
        }
    }
    
}
?>
[/code]
[!--quoteo(post=383534:date=Jun 13 2006, 06:10 PM:name=nogray)--][div class=\'quotetop\']QUOTE(nogray @ Jun 13 2006, 06:10 PM) [snapback]383534[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I don't think $this works in a static function, try to use

page::$lockLevel

instead.
[/quote]

I changed it to page::$lockLevel, then it said I was using an undecared static property. So I changed the variable to a static one. No errors but I set page::$lockLevel on my pages to 4 and it doesn't redirect to login, which it should. Advice?
That happens thanks to the "static" keyword.

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Because static methods are callable without an instance of the object created, the pseudo variable $this is not available inside the method declared as static. [/quote]

[a href=\"http://www.php.net/manual/en/language.oop5.static.php\" target=\"_blank\"]http://www.php.net/manual/en/language.oop5.static.php[/a]
[!--quoteo(post=383899:date=Jun 14 2006, 10:10 AM:name=nogray)--][div class=\'quotetop\']QUOTE(nogray @ Jun 14 2006, 10:10 AM) [snapback]383899[/snapback][/div][div class=\'quotemain\'][!--quotec--]
You are missing a : after Location in your header(), change it like the following.
[code]
header("Location: ".BASE_URL."/login.php");
[/code]
[/quote]
This is not causing the error which is displayed...
But anyway, yet another small mistake.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.