PEAR Auth - call to member function on non-object error

I am trying to use PEAR auth.  The page that requires authentication includes a page that creates the Auth object.  I tested to see if the objects $auth (Auth object) and $dbc (MDB2 object) are set.  $dbc is set, $auth isn't.  Therefore, I get the fatal error "Call to a member function start() on a non-object".

See attached code.

Can anyone tell me why $auth is not set?
//file 1 - requires authentication
require_once('config.inc.php');
if (isset($auth)){
	echo 'auth isset';
}
if (isset($dbc)){
	echo 'dbc isset';
}
$auth->start();
if ($auth->checkAuth()){
//perform main code
}else{
//don't perform main code
}
//END FILE 1
 
//File 2 - config.inc.php
//Auth stuff
 
function logout(){
	if ($auth->checkAuth()){
		$auth->logout();
		$auth->start();
	}
}
function show_login_form(){
	echo '<form method="post" action="login.php" id="login_form">
	<ul>
	<li><label>Username: </label><input type="text" name="username" /></li>
	<li><label>Password: </label><input type="password" name="password" /></li>
	<li><input type="submit" class="submit" value="Login" />
	</ul>
	</form>';
}
$options = array(
	'dsn' => $url,//used by MDB2 object successfully.
	'table' => 'authorised',
	'db_fields' => '*',
	'cryptType' => 'sha1'
	);
$auth = new Auth('MDB2', $options, 'show_login_form');
 
//END FILE 2

Open in new window

deafpandaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Beverley PortlockCommented:
OK - I see two problems here.

In File1 I do not see anything creating $auth - is it supposed to be done elsewhere?

In file 2 the function cannot access a variable or object declared outside its scope. Your function would need amending to


function logout( $auth ){
        if ($auth->checkAuth()){
                $auth->logout();
                $auth->start();
        }
}


or (and this is NOT considered good practice)


function logout(){
 
        global $auth;

        if ($auth->checkAuth()){
                $auth->logout();
                $auth->start();
        }
}

0
deafpandaAuthor Commented:
Thanks for the amendment of the logout function.

the object is created in file 2.  File 1 includes (require_once) file 2.
0
Beverley PortlockCommented:
In that case amend file2 as follows and run it again

$auth = new Auth('MDB2', $options, 'show_login_form');

if ( !isset( $auth ) )
    echo "Failed to create auth object";


You might also want to consider adding

error_reporting( E_ALL );

as the first line of the first script immediately after the <?php tag. Don't worry if it throws a lot of E_NOTICE messages - look for errors instead.
0
deafpandaAuthor Commented:
Right, this is weird.

In file 2 I added the attached code.

'Auth absent' is echoed, despite the fact that require_once('Auth/Auth.php') doesn't throw a fatal error!  What's the problem?
if (file_exists('Auth/Auth.php')){
	echo 'Auth is there ';
}else{
	echo 'Auth absent';
}

Open in new window

0
Beverley PortlockCommented:
file_exists will return FALSE for a lot of reasons - such as wrongly set permissions or it may be that you have safe_mode enabled. For safe_mode run phpinfo() and scan for "safe"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.