Page 1 of 1

PHP Classes Problem!

Posted: Tue Jul 31, 2007 11:09 pm
by cashflowtips
I'm new to PHP but i've been exposed to many other programming languages like C++, Java and etc. I'm having a problem building a classes using PHP. I want to build a module (a group of classes) for authentication (username/password). the main problem here is, how can i build a module which can be used in any system without changing anything on the module itself? in other word is, i want to build a reusable login module that can be used for any system without changing anything on the module. can anyone answer the question or give some example on how to develop this system? thanks. :?:

Posted: Tue Jul 31, 2007 11:44 pm
by yacahuma
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Are you trying to do like a single sign on????


You could create a set of tables that any of your application could use, something similar to this

[syntax="sql"]
CREATE TABLE `application` (
  `appid` int(11) NOT NULL auto_increment,
  `appname` varchar(255) NOT NULL,
  PRIMARY KEY  (`appid`)

CREATE TABLE `user` (
  `userid` int(10) unsigned NOT NULL auto_increment,
  `username` varchar(30) NOT NULL,
  `password` varchar(30) default NULL,
  `fullname` varchar(50) default NULL,
  PRIMARY KEY  (`userid`),
  UNIQUE KEY `username_uni` (`username`)
)
CREATE TABLE `user_app` (
  `appid` int(10) unsigned NOT NULL,
  `userid` int(10) unsigned NOT NULL,
  `credential` varchar(30) default NULL,
  PRIMARY KEY  (`appid`,`userid`)
) 
[/syntax]

Code: Select all

class User
{
   var $userid;
   var $username;
   var $password;
   var $fullname;
}

class UserAuth
{
    function isValid($username,$password,$appname)
   {
       //get user info from sql
        if (found)
         return new User('data from sql');
        else
         return false;
   }
}


then in you login pages

Code: Select all

UserAuth::isValid($username,$password,'APPLICATION_1');

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Aug 01, 2007 3:13 am
by cashflowtips
The process would have to be as follows:

- user requests login with username/password
- login is correct, but password is expired
- user sees message, that password has expired on change-password screen(!) and can enter his old and new password
- the user is forwarded to the requested page.

the major concern here is when the page redirect to change-password screen, how the module suppose to know which page to go without changing anything on the module...? because i want to make it re-usable over and over again to any system without to change anything inside the module...

Posted: Wed Aug 01, 2007 11:31 pm
by cashflowtips
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


i alredy got the idea on how to solve the problem which is : to create an outside parameter - a define in the config or and argument of the calling handler function

Code: Select all

// some included config.php

define("MEMBER_CHANGE_PWD"."http://...");
define("MEMBER_REDIRECT"."http://...");

define("LOGIN_OK","0");
define("LOGIN_FAILED","-1");
define("LOGIN_CHANGE","1");

Code: Select all

// A member class functions

function fnLogin(SOME PARAMS){
    $intLogin=$this->fnCheckLogin();

   if (LOGIN_CHANGE==$intLogin){
        header("Location:MEMBER_CHANGE_PWD");
         exit;
    } elseif (LOGIN_FAILED==$intLogin){
        header("Location:MEMBER_LOGIN_FAILED");        
        exit;
    } else (
// Login OK! do your work

     }

}
how can i put the session on this code so that from this module, the session will keep continue until the user logout or limit specific time if the user idle?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]