PEAR Forum :: PHP Extension and Application Repository

PEAR Forum Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
php::auth invoked from within another class

 
Post new topic   Reply to topic    PEAR Forum Forum Index -> Authentication
View previous topic :: View next topic  
Author Message
Gsheep



Joined: 16 May 2006
Posts: 3

PostPosted: Tue May 16, 2006 9:33 am    Post subject: php::auth invoked from within another class Reply with quote

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
View user's profile Send private message Visit poster's website
Gsheep



Joined: 16 May 2006
Posts: 3

PostPosted: Wed May 17, 2006 12:03 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    PEAR Forum Forum Index -> Authentication All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
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



PEAR Forum topic RSS feed 
Powered by phpBB © 2001, 2005 phpBB Group

Provided by Ministry of Web developement

'Actiemonitor' online projectmanagement software