 |
|
|
| View previous topic :: View next topic |
| Author |
Message |
Gsheep

Joined: 16 May 2006 Posts: 3
|
Posted: Tue May 16, 2006 9:33 am Post subject: php::auth invoked from within another class |
|
|
Could someone help me figure out a problem with getting PEAR::auth to run in a manager class. I've tried for hours to search for existing examples but I can find nought.
I know its already a class but I want to wrap it in a management class so I don't have to keep clogging up pages with invoking the auth() object at the start of the page. I've worked with the [URL="http://pear.php.net/manual/en/package.authentication.auth.intro.php"]examples[/URL] and they work fine, if I wanted to include the code on every login page but I want to further 'abstract' it
heres the class that invokes it:
| Code: |
<?php
require_once("PEAR.php");
require_once("DB.php");
require_once("Auth.php");
require_once("../includes/classes/class_AuthMgr.php");
$AuthMgr = new AuthMgr();
if ($AuthMgr->isLoggedIn())
{
// Do this
}
else
{
// Not authenticated
}
?>
|
and heres the AuthMgr class:
| Code: |
<?php
class AuthMgr{
var $params = array();
function AuthMgr(){
$this->params = array(
"dsn" => "mysql://user:pass@localhost/dbname", //These are just substiutes
"table" => "auth",
"usernamecol" => "username",
"passwordcol" => "password"
);
$this->startSession();
}
function startSession(){
$this->Auth = &new Auth("DB", $params, $this->loginFunction() );
$this->Auth->start();
}
function isLoggedIn(){
switch($this->Auth->getAuth()){
case TRUE: return TRUE;
case FALSE: return FALSE;
}
}
function loginFunction()
{
echo "<form method=\"post\" href= \"".$_SERVER['PHP_SELF']."?act=login\">";
echo "<input type=\"text\" name=\"username\">";
echo "<input type=\"password\" name=\"password\">";
echo "<input type=\"submit\">";
echo "</form>";
}
}
?>
|
The code generates the page fine but just does not ever seem to log in. I know that I should actually be passing a callback to a function rather than the result of the function int the auth(DB, params, "login") but that just plain does'nt work either. Can anyone see any glaring errors in the way I am objectifying it? |
|
| Back to top |
|
 |
Gsheep

Joined: 16 May 2006 Posts: 3
|
Posted: Wed May 17, 2006 12:03 am Post subject: |
|
|
Silly me. I was trying to make an object where there need not be one. All I needed to do was include the actual php file at the top of my page. The I would initialise the auth object from within the auth constructor of the included php file.
| Code: |
<?php
$params = array(
"dsn" "dsn" => "mysql://user:pass@localhost/dbname", //These are just substiutes
"table" => "auth",
"usernamecol" => "username",
"passwordcol" => "password"
);
// A database is used as storage container in this example
$Auth = &new Auth("DB", $params, "loginFunction"); //Take out the "loginFunction" to toggle the default signin
// Detection, if user is logged in. Otherwise the login form
// is being displayed.
$Auth->start();
}
function loginFunction()
{
echo "<form method=\"post\" href= \"".$_SERVER['PHP_SELF']."?act=login\">";
echo "<input type=\"text\" name=\"username\">";
echo "<input type=\"password\" name=\"password\">";
echo "<input type=\"submit\">";
echo "</form>";
}
}
?>
|
So Just to be clear for anyone who may stumble upon this in future. To further call functions from within the included Auth file, lets say for instance you wanted to check if they were logged in, you would do this:
| Code: |
<?php
require_once("PEAR.php");
require_once("DB.php");
require_once("Auth.php");
require_once("../includes/classes/class_AuthMgr.php");
if($Auth->getAuth()){
echo "Still logged in";
}else
{
echo"logged out";
}
?>
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|