Forum Moderators: coopster

Message Too Old, No Replies

Call to a member function on a non-object

         

jackvull

9:02 am on Sep 23, 2005 (gmt 0)

10+ Year Member



Hi
I am getting the following error:
Fatal error: Call to a member function on a non-object in

Below is a stripped down version of the code:
I have a calss in a separate file...
class MakeSecure {

function userInput ($string) {

//Removing anything from the filename string that contains the following characters:
$string = str_replace(';', '', $string); //security
$string = str_replace('#', '', $string); //security
$string = str_replace('=', '', $string); //SQL injection
$string = str_replace('<', '', $string); //SQL injection
$string = str_replace('>', '', $string); //SQL injection
$string = str_replace('"', '', $string); //SQL injection
$string = str_replace('\'', '', $string); //SQL injection
$string = str_replace('%', '', $string); //SQL injection

return $string;

} //end function

} //end class

and then this in the main file that calls it:
<?php include("session.php");

//------------------------------
//Security class and functions
//------------------------------
include("makeSecureClassAndFunctions.php");
$security = new MakeSecure;

[other code]

if (!isset($_GET['page'])) {
$current_page = 1;
}
else {
$current_page = $security->userInput($_GET["page"]);

}

etc.

Now it seems to error on the line calling ->userInput but I can't figure out why. Every other page that cals this class works correctly. I am passing it an integer this time instead of a string but this shouldn't make any difference in PHP should it as it will interpret it as an integer anyway?

Thanks

grandpa

9:09 am on Sep 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$security = new MakeSecure;

Are you doing anything else with the variable $security?

Maybe $security = 'Y'; or something similar? Your error message seems to indicate that the object is no longer available by time you get to the line of code that throws the error.

jackvull

9:27 am on Sep 23, 2005 (gmt 0)

10+ Year Member



I have used it elsewhere in the script for other variables, e.g.
$name = $security->userInput($_POST["name"])

jackvull

9:28 am on Sep 23, 2005 (gmt 0)

10+ Year Member



I don't think that's it though because it works in other scripts where I use it multiple times and I just tried creating a new $securityb = new MakeScure and still get the same error...

grandpa

10:13 pm on Sep 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your usage of the variable in message 3 is fine. I was thinking that if you actually reassigned the variable then you would get this error.

For whatever reason it still appears that your object is losing its reference. Have have tried the script with error reporting on to see if anything shows up?

ergophobe

10:58 pm on Sep 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try accessing it right after creation to see if the object is getting created at all. It could be failing in a few ways OTOMH

- the include fails (relative path problem?)
- the object instantiation fails for some other reason that does not cause a fatal error
- the $security var is getting reset.

Not sure what your error reporting is set to. A failed include only issues a warning, so if you're only reporting errors, that will be silent. If you use require(), it will cause a fatal error on failure, so at least you'll be sure that step succeeded.