Page 1 of 1

[Solved] Whats wrong with $this!

Posted: Sun Dec 21, 2003 9:27 pm
by DuFF
Sorry for the stupid pun in the topic subject :wink:

But right now I'm going crazy trying to understand why I keep getting the error:

Fatal error: Call to a member function on a non-object in /home/duffdesi/public_html/fileshack/uploader/authclass.php on line 47

when trying to use $this in my class.

OK, I have made a user authentication class (class authUser). I made an example page to test out if my errors were working and to my surprise they were not!

Here is the test page (authtest.php)

Code: Select all

<?php
ob_start();  //needed because of headers
include("authclass.php");  //include class script
$user = new authUser;  //start new class
$user->requireAuth(3, "HL");  //call requireAuth function ($requiredlevel, $requiredmod)
echo "You were successfully authenticated!";  //this will only show if requireAuth was successful
?>
I purposefully made the $requiredlevel (3) too high so I would get an error. Here is the actual class:

Code: Select all

<?php
                function requireAuth($reqlvl, $reqmod)
                {
                  session_start();
// some stuff
                  $userlevel = $_SESSION['level'];
                  $usermod = $_SESSION['mod'];
                    if ($userlevel >= $reqlvl)  //if userlevel greater then or equal to required level
                    {
                      if ($usermod == $reqmod || $usermod == "ALL")  //if required mod is correct
                      {
                        return true;  //auth successful
                      }
                      else
                      {
                        $error = "Access Denied:  Incorrect Mod";
                        $this->error($error);
                      }
                    }
                    else
                    {
                      $error = "Access Denied:  Incorrect Userlevel";
                      $this->error($error); // LINE 47!!!
                    }
                }
                function error($error)
                {
                   // simplified code
                   die($error);
                }
?>
For some reason $this in line 47 (commented above) is causing the error message but I have no idea why! Anyone have any ideas? I have tried renaming the error function and some other things but have had no luck.

Posted: Mon Dec 22, 2003 3:24 pm
by DuFF
Well I did some editing and found out that if the line:

Code: Select all

$this->error("Some Error!");
is called before session_start(); it will work but if it is put after session_start(); it will not work. I moved the session_start() out of the function and now it is working correctly.

I still have no idea why though.