?
Solved

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

Posted on 2008-11-11
6
Medium Priority
?
304 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:deafpanda
  • 3
  • 2
5 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 22929564
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
 

Author Comment

by:deafpanda
ID: 22929579
Thanks for the amendment of the logout function.

the object is created in file 2.  File 1 includes (require_once) file 2.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 22929659
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
 

Author Comment

by:deafpanda
ID: 22929660
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
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 2000 total points
ID: 22929742
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question