We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,047 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Php login clss help

hi im trying to use my login class file with my database class file using class extends
and i cant seem to figure out how to connect the two

<?php

 
class access  extends MySQL{
 
    var $user_column = 'username';
    var $email_column = 'email';  
    var $pass_column = 'password';
    var $user_level = 'user_level';
    var $username;
    var $cookie;
 

 
    /**
     * login(): Authenticate the user's username & password
     * @param string $username User's username in the DB
     * @param string $password User's password in the DB
     * @param object $DB    MySQL database class object
     * @param string $table Table to query from
     * @returns bool        True if login successful, false if not
     */
 
    function login ($username, $password, $table) {
        $this->connect("new");
        $this->query("SELECT * FROM member WHERE ".$this->user_column."='$username' AND ".$this->pass_column." = '$password'");
 
        return ( $this->result['password'] == sha1($this->result['username'] . $password) ) ? true : false ;
 
    }
 
    /**
     * login_admin(): Authenticate the an admin's username & password
     * @param string $username User's username in the DB
     * @param string $password User's password in the DB
     * @param object $DB    MySQL database class object
     * @param string $table Table to query from
     * @returns bool        True if login successful, false if not
     */
    function login_admin ($username, $password, $DB, $table = 'users') {
 
        $this->username = $username;
 
        $DB->query("SELECT `username`, `salt`, `password`, `user_level` FROM `$table` WHERE `username` = '$username' LIMIT 1");
 
        if ( ( $DB->result['password'] == sha1($DB->result['salt'] . $password) ) && ( $DB->result['user_level'] ) == '3') {
 
            return true;
 
        } else {
 
            return false;
 
        }
 
    }
 
    /**
     * set_cookie():        Set the user's cookie
     * @param string $cookie_name   Name of the cookie
     * @param object $DB    MySQL database class object
     * @param bool $remember Whether to set for 30 days or just this session
     * @param string $table Table to query from
     * @returns bool        True if cookie was sent, false if not
     */
    function set_cookie ($cookie_name, $DB, $remember = true, $table = 'users') {
 
        $expiration = ( $remember ) ? ( time() + (60 * 60 * 24 * 30) ) : null ;
 
        $DB->query("SELECT * FROM `$table` WHERE `username` = '" . $this->username . "' LIMIT 1");
 
        return ( setcookie($cookie_name, base64_encode( 
                                $DB->result['username'] . ":" .
                                $DB->result['id'] . ":" .
                                $DB->result['user_level'] . ":" .
                                $DB->result['first_name'] . ":" .
                                $DB->result['last_name'] ), $expiration) ) ? true : false ;
 
    }
 
    /**
     * is_logged_in():      Check if user is currently logged in
     * @param string $cookie_name   Name of the cookie
     * @returns bool        True if cookie was sent, false if not
     */
    function is_logged_in ($cookie_name) {
 
        $this->cookie = explode(':', base64_decode($_COOKIE[$cookie_name]) );
 
    }
 
    /**
     * is_admin_logged_in(): Check if user is currently logged in
     * @param string $cookie_name   Name of the cookie
     * @returns bool True if cookie was sent, false if not
     */
    function is_admin_logged_in ($cookie_name) {
 
        if ( isset($_COOKIE[$cookie_name]) ) {
 
            $this->cookie = explode(':', base64_decode($_COOKIE[$cookie_name]) );
 
            if ( $this->cookie[2] == 3 ) {
 
                return true;
 
            } else {
 
                return false;
 
            }
 
        } else {
 
            return false;
 
        }
 
    }
 
    /**
     * logout(): Clear the user's cookie
     * @param string $cookie_name   Name of the cookie
     */
    function logout ($cookie_name) {
 
        setcookie($cookie_name, '');
 
    }
    function loginform($formname, $formclass, $formaction){
        $this->connect("youwebs");
echo '
<form name="'.$formname.'" method=" post" id="'.$formname.'" class="'.$formclass.' login active" enctype="application/x-www-form-urlencoded" action="'.$formaction.'">

             <label style="font-size:10px;">Username:</label>

             <input name="username" id="username" type="text">

            <label style="font-size:10px;">Password: </label>

            <input name="password" id="password" type="password">
            <input name="action" id="action" value="login" type="hidden">
            <input name="action" id="action" value="login" type="hidden">

            <input type="checkbox" /><span  style="font-size:10px;">Keep me logged in</span>

            <input name="submit" id="submit" value="Login" type="submit"></input>
            <label><a href="forgot_password.html" rel="forgot_password" class="forgot linkform" style="font-size:10px;">Forgot your password?</a></label>

	   </form>';
    } 
    function passwordreset($username, $user_table, $pass_column, $user_column){
        //conect to DB
        $this->connect("youwebs");
        //generate new password
        $newpassword = $this->createPassword();
 
        //make sure password column and table are set
        if($this->pass_column == ""){
            $this->pass_column = $pass_column;
        }
        if($this->user_column == ""){
            $this->user_column = $user_column;
        }
        if($this->user_table == ""){
            $this->user_table = $user_table;
        }
        //check if encryption is used
        if($this->encrypt == true){
            $newpassword_db = md5($newpassword);
        }else{
            $newpassword_db = $newpassword;
        }
 
        //update database with new password
        $qry = "UPDATE ".$this->user_table." SET ".$this->pass_column."='".$newpassword_db."' WHERE ".$this->user_column."='".stripslashes($username)."'";
        $result = mysql_query($qry) or die(mysql_error());
 
        $to = stripslashes($username);
        //some injection protection
        $illegals=array("%0A","%0D","%0a","%0d","bcc:","Content-Type","BCC:","Bcc:","Cc:","CC:","TO:","To:","cc:","to:");
        $to = str_replace($illegals, "", $to);
        $getemail = explode("@",$to);
 
        //send only if there is one email
        if(sizeof($getemail) > 2){
            return false;
        }else{
            //send email
            $from = $_SERVER['SERVER_NAME'];
            $subject = "Password Reset: ".$_SERVER['SERVER_NAME'];
            $msg = "
 
Your new password is: ".$newpassword."
 
";
 
            //now we need to set mail headers
            $headers = "MIME-Version: 1.0 rn" ;
            $headers .= "Content-Type: text/html; \r\n" ;
            $headers .= "From: $from  \r\n" ;
 
            //now we are ready to send mail
            $sent = mail($to, $subject, $msg, $headers);
            if($sent){
                return true;
            }else{
                return false;
            }
        }
    }
 
    //create random password with 8 alphanumerical characters
    function createPassword() {
        $chars = "abcdefghijkmnopqrstuvwxyz023456789";
        srand((double)microtime()*1000000);
        $i = 0;
        $pass = '' ;
        while ($i <= 7) {
            $num = rand() % 33;
            $tmp = substr($chars, $num, 1);
            $pass = $pass . $tmp;
            $i++;
        }
        return $pass;
    }
}
 
?>

mysql:

<?php
                          
     class MySQL {
                                         
          protected $connectionData;

          private $connection; 

          private $errorStack;      

          private $queryResult;                                                                                                                          

          public function __construct( $data ){

               $this -> connection = false;

               $this -> errorStack = array();

               $this -> connectionData = array();

               if( is_array( $data ) ){

                    $this -> connectionData = $data;   

               } else $this -> addError("Constructor parameter is invalid");
          }                                           

           public function connect( $dbName = "" ){
               $this -> connection = @mysql_connect( $this -> connectionData["Server"],
                                                     $this -> connectionData["User"],
                                                     $this -> connectionData["Password"] );
               if ( $this -> connection == false ){

                    $this -> addError( "Unable to connect to MySQL server" );
                    return false;
               }

               if( $dbName != "" ){

                   if( @mysql_select_db( $dbName ) == false )

                   $this -> addError( "Unable to select <em>" . $dbName . "</em> database" );  

               } else {
                    if( isset( $this -> connectionData["Database"] ) ){
                         if( $this -> connectionData["Database"] != "" ){
                             if( @mysql_select_db( $this -> connectionData["Database"] ) == false )
                             $this -> addError( "Unable to select <em>" . $dbName . "</em> database" );
                         } 
                    }   
                }         
           }
              
           public function query( $queryString ){
               if( is_string( $queryString ) && $queryString !="" ){
                    $this -> queryResult = @mysql_query( $queryString );
                    if ( $this -> queryResult == false ){
                         $this -> addError( mysql_error() );
                         return false;
                    }
                    return true;
               } else {
                    $this -> addError("Query string is invalid");
                    return false;
               }   
           }      

           public function rowsReturned(){
               if ( $this -> queryResult != false ){
                    return @mysql_num_rows( $this -> queryResult );
               } else {
                    $this -> addError("Execute query before calling rowsReturned()");
                    return false;
               }
           }   

           public function fetchArray(){
               if ( $this -> queryResult != false ){   
                    $data = @mysql_fetch_array( $this -> queryResult );
                    return $data;
               } else {
                    $this -> addError("To get data from database, please execute query fist");
                    return array();
               }    
           }                                                                                                                   

           public function selectDatabase( $dbName ){
               if( $this -> connection != false ){
                    if( is_string( $dbName ) && $dbName !="" ){
                         if(!@mysql_select_db( $dbName )){
                              $this -> addError( "Unable to select <em>" . $dbName . "</em> database" );                              
                         }     
                    } else $this -> addError( "Database name provided is not valid" );  
               } else $this -> addError( "Can not select <em>" . $dbName . "</em> database while connection is closed" ); 
           }                                               

           public function disconnect(){
               if( $this -> connection != false ){
                    @mysql_close( $this -> connection );
               } else $this -> addError("Connection is already closed");
           }                                   

           private function addError( $errorMsg ){
               if( is_string( $errorMsg ) ){
                    array_push( $this -> errorStack, $errorMsg );
               } 
               if( is_array( $errorMsg ) ){
                    foreach( $errorMsg as $error ){
                         array_push( $this -> errorStack, $error );
                    }
               }
           }  

           public function getErrors(){
               if( count( $this -> errorStack ) > 0 ) {
                    $output = "";
                    foreach( $this -> errorStack as $error ){
                         $output .= $error . '<br /';
                    }
                    return $output;
               } else return "There was no errors";   
           }                                                                                                                                                  
          
     }

?>

and login form action:

<?
include("classes/mysql.class.php");
include("classes/actions.class.php");
     $data["Server"] = "localhost";
     $data["User"] = "root";
     $data["Password"] = "*******";
$log = new access($data);
$log->encrypt = true;
if($_REQUEST['action'] == "login"){
    if($log->login("new", $_REQUEST['username'], $_REQUEST['password']) == true){
$user=$_SESSION['username']; 
echo"success $user";

    }else{
     echo"failed!"; 

    }
}
?>

can some one help me please :)

3
Contributors
4
Replies
21 Hours
Discussion Span
1 Year Ago
Last Updated
5
Views
Question
Answered
cleggy1987
Newbie Poster
13 posts since Mar 2011
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

What do you mean by connect the two ? One extends the other, so they are already connected.

pritaeas
Posting Prodigy
Moderator
9,289 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,459
Skill Endorsements: 86

sorry using the login() function i cant access the database login using $this->query(); to return the value of true it will work passing data to and from mysql from a basic php script but getting to connect in another class file is driveing me mad

cleggy1987
Newbie Poster
13 posts since Mar 2011
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

Sorry just scanned the code quickly. What about:

public function __construct(){
  parent::__construct()
}

in your child class? Don't know if that will work.

diafol
Keep Smiling
Moderator
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,510
Skill Endorsements: 57

Its ok i have fixed it now was a combo of lack of sleep and not thinking clearly made me cock up the hole script but a good night sleep helped and now works :) thanks any way lads :)

cleggy1987
Newbie Poster
13 posts since Mar 2011
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0
Question Answered as of 1 Year Ago by pritaeas and diafol

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0806 seconds using 2.81MB