Jump to content

constructor help


chopps

Recommended Posts

Hello all,

 

I have been trying to create a new constructor function using OOP PHP that will automatically encrypt passwords before they are sent to the database.  However, I am not sure if my understanding of constructors is wrong if I am just not doing it right.  As I understand it a constructor function will allow you to perform an operation on an object before the object is used.  I have included some snippets of my code and would appreciate any insight on this matter:

 

Code from the User object is below:

 

    function __construct($encrypt_pass) {

          $this->password = $encrypt_pass;

}

    public function new_user() {

          $create_new_user = new User();

          if(isset($create_new_user->id)) {

            $create_new_user->update();

          } else {

          return false;

    }

  }

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

          global $db;

          $username = $db->escape_value($username);

          $password = $db->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;

}

 

The following code is from the administrative script to create a new user:

 

<?php

if(isset($_POST['submit'])) {

  $create_new_user = new User();

  $create_new_user->username = trim($_POST['username']);

  $create_new_user->password = trim($_POST['password']);

  $create_new_user->first_name = trim($_POST['first_name']);

  $create_new_user->last_name = trim($_POST['last_name']);

  $create_new_user->email = trim($_POST['email']);

 

  if($create_new_user && $create_new_user->create()) {

    $session->message("echo $username has been successfully added as a new us$

    redirect_to("index.php");

} else {

  $message = "There was an error while creating the new user.  Please contact$

}

} else {

  $username ="";

  $password ="";

  $first_name="";

  $last_name="";

  $email="";

}

?>

 

The following snippet is from the login page:

 

if (isset($_POST['submit'])) {

  $username = trim($_POST['username']);

  $password = trim($_POST['password']);

 

  $found_user = User::authenticate($username, $password);

 

if ($found_user) {

  $session->login($found_user);

  log_action('Login', "{$found_user->username} logged in.");

  redirect_to("index.php");

    } else {

    $message = "Username/password combination incorrect.";

  }

} else {

  $username = "";

  $password = "";

}

?>

 

Since I am using the sha algorithm built into PHP I figured it would be best to build it into the object so it would occur automatically. 

Link to comment
Share on other sites

The constructor is called right when you create the object. You can specify as many parameters as you want. In your case, it should look something like this:

<?php
//class constructor
function __construct($encrypt_pass) {
    $this->password = sha1($encrypt_pass);
}

//later on
$password = 'test123';
$user = new User($password);

//Should echo a hash of 'test123'
echo $user->password;

 

It might be worth while to pass an array to the constructor, initializing all of the class variables. Or, creating a populate($array), or setOptions($array) function. The function will iterate through the array, setting up each class variable appropriately.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.