PHP Classes Problem!

Discussion in 'PHP' started by cashflowtips, Jul 31, 2007.

  1. #1
    hello, 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 making any changes on the module. can anyone answer the question or give some example on how to develop this system? thanks.
     
    cashflowtips, Jul 31, 2007 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    There's no problem doing any of that in PHP - the classes follow the same concepts of variables and functions.

    Post as you go and we can help you out.
     
    sarahk, Jul 31, 2007 IP
  3. cashflowtips

    cashflowtips Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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 question is how the module suppose to know where to redirect the page?
     
    cashflowtips, Aug 1, 2007 IP
  4. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    There are a number of ways you can deal with that issue.

    You could, for example, pass the target page around as a parameter. Or, you could store it in a session variable, or in a cookie.
     
    ecentricNick, Aug 1, 2007 IP
  5. cashflowtips

    cashflowtips Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    could u give some example or explain to me how to do it?

    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...
     
    cashflowtips, Aug 1, 2007 IP
  6. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #6
    well you have to give your module some kind of information, or you just use relative links and use the same page names on all your projects: login.php, register.php, login_failed.php etc...
     
    falcondriver, Aug 1, 2007 IP
  7. cashflowtips

    cashflowtips Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    can u explain in more details to me what did u mean by "give a module some kind of information"?
     
    cashflowtips, Aug 1, 2007 IP
  8. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #8
    One way....

    
    //If the user is not logged in...
    if (!myLoginHandler->isUserLoggedIn()){
      //direct them to the login page, passing this page so that the
      //login script can send them back here when logged in
      myLoginHandler->doUserLogin("login.php",$_SERVER['PHP_SELF']);
    }
    
    Code (markup):
     
    ecentricNick, Aug 1, 2007 IP
  9. cashflowtips

    cashflowtips Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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

    // 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 (markup):
    
    // 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
    
         }
    
    } 
    Code (markup):
    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?
     
    cashflowtips, Aug 1, 2007 IP
  10. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #10
    think i can:
    your login page:
    
    <?
    $usr = new User();
    //set parameters
    $usr->page_login = "my_other_loginpage.php";
    $usr->username = getpost("username");
    $usr->password = getpost("password");
    //call login with the given parameters
    $usr->login();
    ?>
    
    PHP:
    your functions.php:
    <?
    
    class User() {
    	var $id = "";
    	var $username = "";
    	var $password = "";
    
    	//redirection targets
    	var $page_login = "login.php";
    	var $page_logout = "logout.php";
    	var $page_wrong_password = "wrong_pass.php";
    	var $page_unknown = "unknown.php";
    
    	function login() {
    		$sql = "select
    				*
    				from
    				users
    				where username like '".$this->username."' and password='".$this->password."'
    				LIMIT 0, 1";
    		//do the query, assume your results ends up in an array "user" after a query
    		if(count($user)==1) {
    			//store your user, redirect
    			$_SESSION['user']=$user;
       			header("Location: ".$this->page_login);
       			exit;
    		}
    		else {
    			//username/password missmatch or unknown user
       			header("Location: ".$this->page_wrong_password);
    		}
    	}	
    }	
    
    function getpost($varname, $default="") {
    	//same with post
    	if (!isset($_POST["$varname"])) $value = $default;
    	else $value = $_POST["$varname"];
    	$value=mysql_real_escape_string(trim($value));
    	return $value;
    }
    ?>
    PHP:
    hope you get the idea here and how to pass variables and values.
    remember: the rep button is the little gray icon :)
     
    falcondriver, Aug 1, 2007 IP