SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    transform php class

    Greetings!
    I'm trying to convert this mysql class to sqlsvr pdo:

    PHP Code:
    $found_user User::authenticate($username$password); 
    PHP Code:
    public static function authenticate($username=""$password="") {
        global 
    $database;
        
    $username $database->escape_value($username);
        
    $password $database->escape_value($password);

        
    $sql  "SELECT * FROM users ";
        
    $sql .= "WHERE username = '{$username}' ";
        
    $sql .= "AND password = '{$password}' ";
        
    $sql .= "LIMIT 1";
        
    $result_array self::find_by_sql($sql);
            return !empty(
    $result_array) ? array_shift($result_array) : false;
        }

     public static function 
    find_by_sql($sql="") {
        global 
    $database;
        
    $result_set $database->query($sql);
        
    $object_array = array();
        while (
    $row $database->fetch_array($result_set)) {
          
    $object_array[] = self::instantiate($row);
        }
        return 
    $object_array;
      }

        private static function 
    instantiate($record) {
            
    // Could check that $record exists and is an array
        
    $object = new self;
            
    // Simple, long-form approach:
            // $object->id                 = $record['id'];
            // $object->username     = $record['username'];
            // $object->password     = $record['password'];
            // $object->first_name = $record['first_name'];
            // $object->last_name     = $record['last_name'];
            
            // More dynamic, short-form approach:
            
    foreach($record as $attribute=>$value){
              if(
    $object->has_attribute($attribute)) {
                
    $object->$attribute $value;
              }
            }
            return 
    $object;
        } 
    My problem is, is there any equivalent oop pdo classes from this?.I decided to use the class which is I think efficient and flexible. I haven't tested my code but this is my initial try.

    PHP Code:
    public static function authenticate($username="",$password="")
     {

     
    SQLDatabase::conn()
     
    $sql 'SELECT id,username, password*
    FROM ip.tbl_user
     WHERE username= :uname
     AND password= :pass '
    ;
        
        
         
    $result_array self::find_by_sql($sql);
            return !empty(
    $result_array) ? array_shift($result_array) : false;
        }

     public static function 
    find_by_sql($sql="") {

     
    $st SQLDatabase::$db->prepare($sql);
     
    $st->bindParam(':uname'$username);
    $st->bindParam(':pass'$password);
    $st->execute();

     
    $object_array = array();
     
    $row $st->rowCount();
    while (
    $row $st->fetch(PDO::FETCH_ASSOC)) {
     
    $object_array[] = self::instantiate($row);
     }
     return 
    $object_array;

     }

    private static function 
    instantiate($record) {
            
    // Could check that $record exists and is an array
     
    $object = new self;
            
    // Simple, long-form approach:
            // $object->id                 = $record['id'];
            // $object->username     = $record['username'];
            // $object->password     = $record['password'];
            // $object->first_name = $record['first_name'];
            // $object->last_name     = $record['last_name'];
            
            // More dynamic, short-form approach:
            
    foreach($record as $attribute=>$value){
             if(
    $object->has_attribute($attribute)) {
            
    $object->$attribute $value;
            }
            }
            return 
    $object;
        } 

    Any help will do. thank you .

  2. #2
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This doesn't use static functions/variables, but I think this is a neat way of processing user data:

    First I have a class called Member.php:

    PHP Code:
    <?php 

    class Member {
        
        
    // The member attributes containing required and optional information.
        // The attributes must correspond to the database table columns:
        
        
    private $id NULL;
        private 
    $userType=NULL// Required (assigned enum)
        
    private $username=NULL// Required 
        
    private $email=NULL// Required   
        
    private $pass=NULL// Required
        
    private $fullName=NULL;
        private 
    $address=NULL;
        private 
    $city=NULL;
        private 
    $state=NULL;
        private 
    $zipCode=NULL;
        
        
    // Method returns the user ID:
        
    public function getId() {
            return 
    $this->id;
        }
        
    // Grab the password:
        
    public function getPass() {
            return 
    $this->pass;    
        }
        
        
    // Clear the password once user is logged in:
        
    public function clearPass() {
            
    $this->pass NULL;    
        }
        
        
    // Method returns a Boolean if the user is an administrator:
        
    public function isAdmin() {
            return (
    $this->userType == 'admin');
        }
        
        
    // Method returns a Boolean indicating if the user is an administrator
        // or if the user is the original author of the provided page:
        
    public function canEditPage(Page $page) {
            return (
    $this->isAdmin() || ($this->id == $page->getCreatorId()));
        }
        
        
    // Method returns a Boolean indicating if the user is an administrator or an author:
        
    public function canCreatePage() {
            return (
    $this->isAdmin() || ($this->userType == 'author'));
        }
          
    }
    and in my login.php script (partial code) I do:
    PHP Code:
    if ($form->validate()) {

        
    // Check against the database:
        
    $query 'SELECT id, userType, username, email, pass, fullName, address, city, state, zipCode FROM users WHERE username=:username';
        
    $stmt $pdo->prepare($query);
        
    $result $stmt->execute(array(':username' => $fetched_user_data->getValue()));
                
        
    // Try to fetch the results:
        
    if ($result) {
            
    $stmt->setFetchMode(PDO::FETCH_CLASS'Member');  // This Fetches the data and 
            
    $stored_user_data $stmt->fetch();
            
    $result false;
        }
        
        
    // Verify Stored Hashed Password:
        
    $result password_verify($password->getValue(), $stored_user_data->getPass());
        
        
    // Cheched User's entered password against stored password and redirect if they match:
        
    if ($result) {

             
    // Once User is authenticated there is no need to store password in $_SERVER['user']:
            
    $stored_user_data->clearPass();
            
            
    // Store user's info (minus password) in the session:
            
    $_SESSION['user'] = $stored_user_data;
               
            
    //Redirect:
            
    header("Location:index.php");
            exit;
            
        } 
    You could probably modify the code to match your coding style? I don't know if this helps or not?

  3. #3
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I don't know if this helps or not?
    This thing is good. It might not the thing I really want but it will really helps. I'm going to study it. Thanks man. thank you.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •